【问题标题】:boosting along with prefix query with all与所有前缀查询一起提升
【发布时间】:2015-02-14 06:15:18
【问题描述】:

我希望能够为在任何字段中找到的每个搜索词添加前缀查询,并且我希望能够突出显示。我制定了一个似乎有效的查询。现在,我想更新查询,以便其中一个字段中的匹配产生比其他字段中的匹配更高的分数。

例如我索引以下数据(这只是一个示例,在我的真实数据中,除了两个之外还有更多字段):

PUT /my_index/my_type/abc124 { “标题”:“废话”, “描述”:“高尔夫” }

PUT /my_index/my_type/abc123 { “标题”:“等等高尔夫”, “描述”:“课程” }

PUT /my_index/my_type/abc125 { "title" : "blah 高尔夫球座", “描述”:“课程” }

然后我可以使用如下查询进行查询:

POST my_index/my_type/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "prefix": {
                        "_all" : "gol"
                    }    
                },
                {
                    "prefix": {
                        "_all": "bla"
                    }
                }
            ]
        }
    },
      "highlight":{
    "fields":{
      "*":{}
    }
  }
}

产生结果:

{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 4,
      "successful": 4,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1.4142135,
      "hits": [
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "abc125",
            "_score": 1.4142135,
            "_source": {
               "title": "blah golf tee",
               "description": "course"
            },
            "highlight": {
               "title": [
                  "<em>blah</em> <em>golf</em> tee"
               ]
            }
         },
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "abc124",
            "_score": 1.4142135,
            "_source": {
               "title": "blah",
               "description": "golf"
            },
            "highlight": {
               "description": [
                  "<em>golf</em>"
               ],
               "title": [
                  "<em>blah</em>"
               ]
            }
         },
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "abc123",
            "_score": 1.4142135,
            "_source": {
               "title": "blah golf",
               "description": "course"
            },
            "highlight": {
               "title": [
                  "<em>blah</em> <em>golf</em>"
               ]
            }
         }
      ]
   }
}

如何使用 function_score 或其他方式修改评分,以便我可以在 title 字段上的匹配得分高于其他字段?我是否需要将查询更改为多重匹配而不是使用 _all?任何建议将不胜感激。

问候, LT

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    尝试在您的bool 查询中添加一个should 部分,如果should 中的任何语句匹配,则该部分将为整个查询提供更高的分数(并且对于那些与查询匹配的语句不是强制性的返回结果)。

    例如,试试这个:

    POST my_index/my_type/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "prefix": {
                "_all": "gol"
              }
            },
            {
              "prefix": {
                "_all": "bla"
              }
            }
          ],
          "should": [
            {
              "prefix": {
                "title": {
                  "value": "gol",
                  "boost": 3
                }
              }
            },
            {
              "prefix": {
                "title": {
                  "value": "bla",
                  "boost": 3
                }
              }
            }
          ]
        }
      },
      "highlight": {
        "fields": {
          "*": {}
        }
      }
    }
    

    【讨论】:

    • 非常感谢 Andrei。这正是我正在寻找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多