2014-08-29 174 views
3

我有一個查詢,我無法完全回答最相關的答案。在搜索結果開始時使用匹配搜索詞,而不是在搜索結果中使用elasticsearch

如果我搜索 'HERP' 的結果是

  • 福HERP
  • HERP DERP
  • 酒吧HERP
  • HERP AA

優選謹以此順序

  • HERP AA
  • HERP DERP
  • 酒吧HERP
  • 福HERP

所以我有問題,我想的是:我如何條款中的單詞序列的開始獲得更高比一個看起來更接近尾聲的得分?

我用一下這樣的分析:

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

和映射看起來像這樣(場的魯特但阿拉看起來是一樣的)

"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

實現此目的的一種方法是爲字段開頭的術語提供額外的提升使用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 
     } 
    } 
}