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

时间:2014-08-29 13:45:15

标签: search lucene elasticsearch

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

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

  • fu herp
  • herp derp
  • bar herp
  • herp aa

最好是我想要这个订单

  • herp aa
  • herp derp
  • bar herp
  • 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"
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

实现此目标的一种方法是使用span first

在字段开头为术语提供额外的提升

示例:

{
   "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
      }
   }
}