简体   繁体   English

在弹性搜索中实现优先级搜索

[英]Implementing priority search in elastic search

I'm trying to implement custom search in elastic search. 我正在尝试在弹性搜索中实现自定义搜索。

Problem statement is consider 3 documents inserted into elastic search with "names" field as array: 问题陈述是考虑将3个文档插入到具有“名称”字段作为数组的弹性搜索中:

{
   id:1,
   names:["John Wick","Iron man"]
}
{
   id:2,
   names:["Wick Stone","Nick John"]
}
{
   id:3,
   names:["Manny Nick","Stone cold"]
}

when I search for "Nick" I want to boost or give priority to document starting with Nick so in this case document with id 2 should come first and then document with id 3 and also if I search for whole name "Manny Nick" doc with id 3 should be given priority. 当我搜索“ Nick”时,我想提升或优先使用以Nick开头的文档,因此在这种情况下,应先输入ID 2的文档,然后再输入ID 3的文档,如果我搜索全名“ Manny Nick”的文档, ID 3应该优先。

In such case, you may want to modify/boost the score of search matched result for required criteria. 在这种情况下,您可能需要针对所需条件修改/提高搜索匹配结果的分数 For example, match the documents with names "Nick" and at the same time modify and boost the score of documents which contains names that start with Nick so that documents that match Nick and also starts with Nick will have higher score . 例如,匹配名的文件"Nick" ,并在同一时间修改和增加的文档的分数包含names与启动Nick使匹配文件Nick也开始与Nick将有更高的分数

One of the way to achieve this is using Function Score Query. 实现此目的的一种方法是使用功能得分查询。 In the below query, search is made for keyword "Nick" and matched documents' score is modified and boosted for criteria "names that start with Nick" using Match Phrase Prefix Query with additional weight 20. 在下面的查询中,使用匹配词组前缀查询 (附加权重为20)搜索关键字“ Nick”,并对匹配文档的分数进行修改并提高标准“以Nick开头的名称”

{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "names": "Nick"
        }
      },
      "boost": "1",
      "functions": [
        {
          "filter": {
            "match_phrase_prefix": {
              "names": "Nick"
            }
          },
          "weight": 20
        }
      ],
      "boost_mode": "sum"
    }
  }
}

Testing: 测试:

Inserted data: 插入的数据:

{
   id:1,
   names:["John Wick","Iron man"]
}
{
   id:2,
   names:["Wick Stone","Nick John"]
}
{
   id:3,
   names:["Manny Nick","Stone cold"]
}

Output: 输出:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 20.693148,
        "hits": [
            {
                "_index": "stack_1",
                "_type": "1",
                "_id": "T9kn5WsBrk7qsVCmKBGH",
                "_score": 20.693148,
                "_source": {
                    "id": 2,
                    "names": [
                        "Wick Stone",
                        "Nick John"
                    ]
                }
            },
            {
                "_index": "stack_1",
                "_type": "1",
                "_id": "Ttkm5WsBrk7qsVCm2RF_",
                "_score": 20.287682,
                "_source": {
                    "id": 3,
                    "names": [
                        "Manny Nick",
                        "Stone cold"
                    ]
                }
            }
        ]
    }
}

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

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