简体   繁体   English

如何根据相关性对Elastic Search查询结果进行排序?

[英]How can I sort Elastic Search query results based on relevancy?

I am trying to do a search in elastic search server. 我正在尝试在elastic search服务器中进行elastic search

Below is my case: 以下是我的情况:

Given a term "Hello World" to the search API, return me all the documents with: 给搜索API一个术语"Hello World" ,返回所有文档:

  1. exact pattern "Hello World" 确切模式“Hello World”
  2. "Hello" AND "world" “你好”和“世界”
  3. "Hello" OR "world" “你好”或“世界”

I want to do the above in a single query. 我想在一个查询中执行上述操作。 I am aware that individually all of them can be done using match_phrase, and default_operator for OR/AND. 我知道,所有这些都可以使用match_phrase和default_operator进行OR / AND。 But I want all the three to be done in a single query. 但我希望所有这三个都在一个查询中完成。

I want the results to be sorted based on relevancy. 我希望根据相关性对结果进行排序。 So if a document contains the exact phrase, it is most relevant.If the document contains both the words (AND) some where in it, it is moderately relevant. 因此,如果文档包含确切的短语,则它是最相关的。如果文档中包含单词(AND),则其中的某些位置具有中等相关性。 And if the document contains at least one of the word(OR), it is least relevant. 如果文档包含至少一个单词(OR),则它最不相关。

Is it possible in elastic search as of now ? 到目前为止,弹性搜索是否可行?

This is something you might require: 这是您可能需要的:

Query String 请求参数

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

{
  "query": 
   {
    "query_string" : 
       {
        "query" : "Hello World"
       }
   }
}

This will fetch you all combinations : 这将获取所有组合:

  • Hello, World, Hello World, World Hello. 你好,世界,你好世界,世界你好。

You want to check Bool Query and possibly Boosting Query Clauses . 您想要检查Bool查询以及可能的Boosting Query子句

For your example, you can do 举个例子,你可以做到

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "FIELDNAME": {
                            "query": "Hello",
                            "boost": 1
                        }
                    }
                },
                {
                    "match": {
                        "FIELDNAME": {
                            "query": "World",
                            "boost": 1
                        }
                    }
                },
                {
                    "match_phrase": {
                        "FIELDNAME": {
                            "query": "Hello World",
                            "boost": 2
                        }
                    }
                }
            ],
            "minimum_should_match" : 1
        }
    }
}

This is asking that at least 1 between your three conditions is true ( minimum_should_match ). 这要求您的三个条件之间至少有1个为真( minimum_should_match )。 The more conditions are satisfied, the higher score the document will have (so it will be returned first), like you wished. 满足的条件越多,文档的得分就越高(因此它将首先返回),就像您希望的那样。 You can boost the importance of a condition, here I went for doubling the importance of whole phrase match, but it's just an example. 你可以boost条件的重要性,在这里我将整个短语匹配的重要性加倍,但这只是一个例子。

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

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