简体   繁体   English

根据字段值对Elasticsearch结果进行排序

[英]Sort Elasticsearch results based on field value

Assuming I have 3 documents (users), and they have knowledge of multiple programming languages - with scores associated, as described below, how can I search for multiple fields (multi-match for example), and if some search-keywords hits a language, sort by its score? 假设我有3个文档(用户),并且他们具有多种编程语言的知识-与分数相关联,如下所述,我如何搜索多个字段(例如,多重匹配),以及某些搜索关键字是否符合一种语言,按分数排序?

    // user1 
    {
        "name": "John Bayes",
        "prog_langs": [
            {
                "name": "python",
                "score": 10
            },
            {
                "name": "java",
                "score": 500
            }
        ]
    }        



    // user2 
    {
        "name": "John Russel",
        "prog_langs": [
            {
                "name": "python",
                "score": 100
            },
            {
                "name": "PHP",
                "score": 200
            }
        ]
    }        



    // user3
    {
        "name": "Terry Guy",
        "prog_langs": [
            {
                "name": "C++",
                "score": 600
            },
            {
                "name": "Javascript",
                "score": 200
            }
        ]
    }

For example: searching "John python" Should return user1 and user2, but user2 showing up first 例如:搜索“ John python”应返回user1和user2,但user2首先显示


**I've been trying to use sort and functions, but I think they always use lowest/highest/average values of score. **我一直在尝试使用排序和函数,但我认为它们始终使用得分的最低/最高/平均值。

Thanks! 谢谢!


[Edit] **In the meantime I got it working in a testing way to see if without full-text/multi-matched works, and I found out I had to make "prog_langs" nested, so I changed the mapping and it works as expected. [编辑] **同时,我以一种测试的方式使其工作,以查看是否没有全文/多匹配的作品,并且我发现我必须嵌套“ prog_langs”,因此我更改了映射并可以正常工作如预期的那样。

Now I'm only missing the part where a full-text search with multi-match merges with current query. 现在,我仅缺少具有多匹配项的全文搜索与当前查询合并的部分。

Thanks again! 再次感谢!

I managed to fix the query and now it's working as expected. 我设法解决了该查询,现在它可以按预期运行。

Before posting my solution, just have to leave a few things to keep in mind: 在发布我的解决方案之前,只需记住以下几点:

  • I made a new mapping, and added some nested objects, so my original query had to suffer some changes (prog_langs are now of type nested ) 我做了一个新的映射,并添加了一些嵌套的对象,因此我的原始查询必须进行一些更改(prog_langs现在是nested的类型)
  • I wanted at least two fields to match, being mandatory which should match at least once 我希望至少两个字段匹配,这是必填字段,至少应该匹配一次

    {
        "query": {
            "bool": {
                "must": [
                    {
                        "query": {
                            "match": {
                                "name": {
                                    "query": "john python",
                                    "boost": 5
                                }
                            }
                        }
                    },
                    {
                        "bool": {
                            "should": [
                                {
                                    "nested": {
                                        "path": "prog_langs",
                                        "query": {
                                            "match": {
                                                "prog_langs.name": {
                                                    "query": "john python",
                                                    "boost": 5
                                                }
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                ],
                "should": [
                    {
                        "function_score": {
                            "query": {
                                "match": {
                                    "prog_langs.name": "john python"
                                }
                            },
                            "functions": [
                                {
                                    "script_score": {
                                        "script": "_score * (1 + doc['prog_langs.score'].value)"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        },
        "highlight": {
            "fields": {
                "name": {},
                "prog_langs.name": {}
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM