Elasticsearch结果排序顺序

时间:2019-03-12 07:49:11

标签: elasticsearch

我想按特定顺序获得结果,首先要获得完全匹配的词组(match_phrase),然后匹配词组中的任何单词(match),例如,如果我要搜索“我在哪里可以找到我的帐户”我将首先获取包含完整短语“我在哪里可以找到我的帐户”的文档,然后获取包含一个或多个单词“ where”,“ can”,“ find”,“ my”,“ account”的文档

我的查询:

GET my_index/_search

{

"query": {

"bool": {
  "should": [
    {
      "match": {"body": "right usage of Localization"      }
    } ,
    {
      "match": {"title": "right usage of Localization"      }
    } 
    ],
    "should": [
    {
      "match_phrase": {"body": "translated"      }
    },
    {
      "match_phrase": {"title": "translated"      }
    } 
    ]
  }

} }

1 个答案:

答案 0 :(得分:0)

您可以使用boost query来增强与match_phrase查询匹配的文档。 您可以相应地更改boost参数。

以下查询应为您工作

POST phrase_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "title": "this is where it should work first"
                }
              },
              {
                "match": {
                  "body": "this is where it should work first"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "title": {
                    "query": "this is where it should work first",
                    "boost": 20
                  }
                }
              },
              {
                "match_phrase": {
                  "body": {
                    "query": "this is where it should work first",
                    "boost": 20
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}