2016-07-19 40 views
1

私はPythonでElastic検索を使用しています。私はアクセントで鈍感な検索をする方法を見つけることができません。弾性検索非表示検索アクセント

例: 2つの単語があります。 "カミオン"及び "カムニオン"。 ユーザーが「camion」を検索すると、2つの結果が表示されます。

作成インデックス:

es = Elasticsearch([{u'host': u'127.0.0.1', u'port': b'9200'}]) 

es.indices.create(index='name', ignore=400) 

es.index(
    index="name", 
    doc_type="producto", 
    id=p.pk, 
    body={ 
     'title': p.titulo, 
     'slug': p.slug, 
     'summary': p.summary, 
     'description': p.description, 
     'image': foto, 
     'price': p.price, 
     'wholesale_price': p.wholesale_price, 
     'reference': p.reference, 
     'ean13': p.ean13, 
     'rating': p.rating, 
     'quantity': p.quantity, 
     'discount': p.discount, 
     'sales': p.sales, 
     'active': p.active, 
     'encilleria': p.encilleria, 
     'brand': marca, 
     'brand_title': marca_titulo, 
     'sellos': sellos_str, 
     'certificados': certificados_str, 
     'attr_naturales': attr_naturales_str, 
     'soluciones': soluciones_str, 
     'categories': categories_str, 
     'delivery': p.delivery, 
     'stock': p.stock, 
     'consejos': p.consejos, 
     'ingredientes': p.ingredientes, 
     'es_pack': p.es_pack, 
     'temp': p.temp, 
     'relevancia': p.relevancia, 
     'descontinuado': p.descontinuado, 
    } 

検索:私はグーグル、stackoverflowのとelastic.coで検索しましたが、私は働く何かを見つけることができませんでした

from elasticsearch import Elasticsearch 
    es = Elasticsearch([{'host': '127.0.0.1', 'port': '9200'}]) 

    resul = es.search(
     index="name", 
     body={ 
      "query": { 
       "query_string": { 
        "query": "(title:" + search + " OR description:" + search + " OR summary:" + search + ") AND (active:true)", 
        "analyze_wildcard": False 
       } 
      }, 
      "size": "9999", 
     } 
    ) 
    print resul 

+1

を、あなたのクエリで使用これらのフィールドのマッピングは何ですか? –

+0

あなたはデータベースを意味しますか?すべての文字列。クエリで何か宣言する必要はありますか? –

+0

どのデータベースですか? :-) –

答えて

2

クエリに含まれているフィールドのマッピングを変更する必要があります。マッピングを変更するには、フィールドの分析方法が異なるようにインデックスを再作成する必要があり、クエリが機能します。

基本的には、以下のようなものが必要です。 textというフィールドは単なる例です。他のフィールドにも同じ設定を適用する必要があります。 fieldsを使用して、ルートフィールドがデフォルトで分析された元のテキストを維持するようにし、text.foldedはアクセント記号付きの文字を削除し、クエリが機能するようにします。私はそのフィールドの両方のバージョン(camionが一致するだけでなく、camión)も検索できるように、クエリを少し変更しました。

PUT /my_index 
{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "folding": { 
      "tokenizer": "standard", 
      "filter": [ 
      "lowercase", 
      "asciifolding" 
      ] 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "test": { 
     "properties": { 
     "text": { 
      "type": "string", 
      "fields": { 
      "folded": { 
       "type": "string", 
       "analyzer": "folding" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

と問合せ:

"query": { 
    "query_string": { 
     "query": "\\*.folded:camion" 
    } 
    } 

また、私は強くドキュメントのこのセクションを読んでお勧め:https://www.elastic.co/guide/en/elasticsearch/guide/current/asciifolding-token-filter.html

+0

私はこのようなものを見ました。しかし、私はこのコードをどこに置くのですか? es.index()のbody {}の前に? –

+1

私はPythonを知らない。ごめんなさい。私が提供したコードは、それらの設定とそのマッピングでインデックスを作成します。だから、既存のインデックスを削除する必要があります、私は新しいインデックスを作成するために使用されたコードを再インデックスする必要があります。 –