2017-12-16 22 views
0

私はElasticSearchを初めて使用していて、Elasticsearchを設定してファジーマッチを得ようとしました。ファジー検索、オートコンプリートフィルター、およびシングルを実装すると、完全一致は部分一致よりもスコアが低いようです。たとえば、クエリが "Ring"の場合、 "Ring"の代わりに "Brass Ring"との一致が高いようです。ファジー一致が完全一致より高いスコアを得ました

誰でもお手伝いできますか?ここで

は、私は、インデックスを作成する方法である:ここでは

itemindex = es.indices.create(
     index='mo-items-index-1', 
     body={ 
     "settings": { 
      "number_of_shards": 1, 
      "analysis": { 
       "filter": { 
        "autocomplete_filter": { 
         "type":  "edge_ngram", 
         "min_gram": 1, 
         "max_gram": 20 
        }, 
        "custom_shingle": { 
         "type": "shingle", 
         "min_shingle_size": 2, 
         "max_shingle_size": 3, 
         "output_unigrams": True 

        }, 
        "my_char_filter": { 
         "type": "pattern_replace", 
         "pattern": " ", 
         "replacement": "" 
        } 
       }, 
       "analyzer": { 
        "autocomplete": { 
         "type":  "custom", 
         "tokenizer": "standard", 
         "filter": [ 
          "lowercase", 
          "custom_shingle", 
          "autocomplete_filter", 
          "my_char_filter" 
         ] 
        } 
       } 
      } 
     }, 
     "mappings": { 
     "my_type": { 
      "properties": { 
       "item_id": { 
        "type":  "string", 
        "analyzer": "autocomplete", 
      "search_analyzer": "standard" 

       }, 
      "item_name": { 
        "type":  "string", 
        "analyzer": "autocomplete", 
      "search_analyzer": "standard" 

       } 
      } 
     } 
    } 
     }, 
     # Will ignore 400 errors, remove to ensure you're prompted 
     ignore=400 
    ) 

は、私は言葉を照会する方法である:

res2 = es.search(index="mo-items-index-1", size=200, body={"query": {"multi_match": { 
     "fields": [ 
      "item_name", "item_id"], "query": userQuery, "fuzziness": "AUTO"}}, "highlight": { 

     "fields": { 
      "item_name": {}, 
      "item_id": {} 

     } 
    }, }) 

答えて

1

完全一致のスコアを「後押し」するための非常に簡単な方法があります:既に存在するクエリを使用するboolクエリとクエリ内部を使用します。

"query": { 
    "bool": { 
     "should": [ 
     { 
      "multi_match": { 
      "fields": [ 
       "item_name", 
       "item_id" 
      ], 
      "query": "Ring", 
      "fuzziness": "AUTO" 
      } 
     }, 
     { 
      "term": { 
      "item_name.keyword": { 
       "value": "Ring" 
      } 
      } 
     } 
     ] 
    } 
    } 

そして、あなたは、あなたがのために完璧にマッチを好むしたいフィールドにサブフィールドのkeywordタイプを追加することもあるだろう:

"mappings": { 
    "my_type": { 
     "properties": { 
     "item_id": { 
      "type": "string", 
      "analyzer": "autocomplete", 
      "search_analyzer": "standard" 
     }, 
     "item_name": { 
      "type": "string", 
      "analyzer": "autocomplete", 
      "search_analyzer": "standard", 
      "fields": { 
      "keyword": { 
       "type": "keyword" 
      } 
      } 
     } 
     } 
    } 
    } 
+0

は、あなたの答えをありがとう、私は「ピン」などのアイテムを持っている、「ミッキーを!ピン "、"ブルーピン "と"キャップ "、"野球帽 "、"赤い帽子 "ので、私はピン、キャップなどをitem_name.keywordの下の"値 "フィールドに追加する必要がありますか? "value"のようなもの:["Ring"、 "Pin"、 "Cap"]?事前に感謝します – Abhijay

+0

あなたの質問で判断すると、あなたはElasticsearchに関するドキュメントを読んでいませんでした。基本的ではない。サブフィールドの場合、何もする必要はありません。 Elasticsearchはサブフィールドを自動的に設定します。キバナの開発ツール(私がお勧めします)を使用している場合は、マッピング、データ、クエリを使って完全なテストを行っています:https://gist.github.com/astefan/f3e8f9ff870c5efed860588186af7e14 –

+0

はい、私はドキュメンテーション。この設定では、「リング」という用語にクエリを実行すると、インデックスに「リング」が存在する場合、先頭の結果が「センタリング」になります。何が起きているのか教えていただけますか? – Abhijay

関連する問題