【问题标题】:Elasticsearch results sort orderElasticsearch 结果排序顺序
【发布时间】:2019-03-12 07:49:11
【问题描述】:

我想按特定顺序获取结果,首先获取确切的短语 (match_phrase) 匹配,然后匹配短语中的任何单词 (match),例如,如果我正在搜索“我在哪里可以找到我的帐户”我将首先获取包含完整短语“where can I find my account”的文档,然后获取包含一个或多个单词“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"      }
    } 
    ]
  }

} }

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用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
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 嗨!如果我只想对两个字段都使用 match_phrase 怎么办?我在没有任何 match 的情况下尝试了您的答案,但它对我不起作用。
    • 你用的是什么ES版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多