繁体   English   中英

在搜索结果的开头而不是最终使用elasticsearch匹配搜索词

[英]Prefer matching search term in beginning of search result rather than in the end using elasticsearch

我有一个查询,我无法回答最相关的答案。

如果我搜索'herp',结果是

  • fu herp
  • herp derp
  • 酒吧
  • herp aa

我最喜欢这个订单

  • herp aa
  • herp derp
  • 酒吧
  • fu herp

所以我的问题是,我猜测的是:如何在一系列单词的开头获得更高的分数,而不是更接近结尾的分数?

我使用的分析器看起来像这样:

"analyzer":[
  "autocomplete":[
    "type":"custom",
    "tokenizer":"standard",
    "filter":[ 
      "standard",
      "lowercase",
      "stop",
      "edgeNGramAlpha" 
    ]
  ],
  "filter":[
    "edgeNGramAlpha":[
      "type":"edgeNGram",
      "min_gram":1,
      "max_gram":20
    ]
  ]
]

并且映射看起来像这样(战场但是alla看起来一样)

"name": [
  "type": "multi_field",
  "fields" : [
    "untouched": [
      "type": "string",
      "index": "not_analyzed"
    ],
    "name": [
      "type": "string"
    ],
    "autocomplete": [
      "analyzer":"${language}_autocomplete",
      "type":"string",
    ]
  ]
]

并且查询看起来像这样:

{
  "from": 0,
  "size": 10,
  "query": {
    "filtered": {
      "query": {
        "multi_match": {
          "query": "herp",
          "fields": [
            "name^8",
            "name.autocomplete^4",
            "historic_name.autocomplete"
          ],
          "type": "cross_fields",
          "operator": "AND",
          "analyzer": "standard"
        }
      }
    }
  }
}

实现这一目标的一种方法是首先使用跨度为字段开头的术语提供额外的增强

示例:

{
   "from": 0,
   "size": 10,
   "query": {
      "bool": {
         "disable_coord": true,
         "must": [
            {
               "multi_match": {
                  "query": "herp",
                  "fields": [
                     "name^8",
                     "name.autocomplete^4",
                     "historic_name.autocomplete"
                  ],
                  "analyzer": "standard"
               }
            }
         ],
         "should": [
            {
               "span_first": {
                  "match": {
                     "span_term": {
                        "name": "herp"
                     }
                  },
                  "end": 1,
                  "boost": 1
               }
            },
            {
               "span_first": {
                  "match": {
                     "span_term": {
                        "historic_name": "herp"
                     }
                  },
                  "end": 1,
                  "boost": 1
               }
            }
         ],
         "minimum_number_should_match": 0
      }
   }
}

暂无
暂无

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

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