2017-01-15 9 views
0

私はelasticsearchを初めて使っています。私の索引にはタイトルのある項目があります:ElasticsearchでPythonを使う 私が検索すると、「ElasticsearchでPythonを使う」ということを除いて、常にゼロヒットになります。 様:python elasticserch:完全一致のみ結果を返すことができます

1、コード

import elasticsearch 
    INDEX_NAME= 'test_1' 
    from elasticsearch import Elasticsearch 
    es = Elasticsearch() 
    es.index(index=INDEX_NAME, doc_type='post', id=1, body={'title': 'Using Python with Elasticsearch', 'tags': ['python', 'elasticsearch', 'tips'], }) 
    es.indices.refresh(index=INDEX_NAME) 
    res = es.indices.get(index=INDEX_NAME) 
    print res 

出力は次のとおり

INDEX_NAME = 'test_1' 
from elasticsearch import Elasticsearch 
es = Elasticsearch() 
request_body = { 
'mappings':{ 
'post': { 
'properties': { 
'title': {'type':'text'} 
} 
} 
} 
} 
if es.indices.exists(INDEX_NAME): 
res = es.indices.delete(index = INDEX_NAME) 
print(" response: '%s'" % (res)) 
res = es.indices.create(index = INDEX_NAME, body= request_body, ignore=400) 
print res 

{u'test_1': {u'warmers': {}, u'settings': {u'index': {u'number_of_replicas': u'1', u'number_of_shards': u'5', u'uuid': u'Z2KLxeQLRay4rFgdK4yo9A', u'version': {u'created': u'2020199'}, u'creation_date': u'1484405704970'}}, u'mappings': {u'post': {u'properties': {u'blog': {u'type': u'string'}, u'title': {u'type': u'string'}, u'tags': {u'type': u'string'}, u'author': {u'type': u'string'}}}}, u'aliases': {}}} 

2、私は以下のコードとのマッピングを変更します

出力Iが1.9からelasticsearchを更新し、

response: '{u'acknowledged': True}' 
{u'status': 400, u'error': {u'caused_by': {u'reason': u**'No handler for type [text] declared on field [title]**', u'type': u'mapper_parsing_exception'}, u'root_cause': [{u'reason': u'No handler for type [text] declared on field [title]', u'type': u'mapper_parsing_exception'}], u'type': u'mapper_parsing_exception', u'reason': u'Failed to parse mapping [post]: No handler for type [text] declared on field [title]'}} 

3(5、1、0、 'DEV')

4、Iはまた、以下のコードでマッピングを変更しようとします

request_body = { 
'mappings':{ 
'post': { 
'properties': { 
**'title': {'type':'string', "index": "not_analyzed"}** 
} 
} 
} 
} 

5私も、このようにマッピングを変更:

request_body = { 
'mappings':{ 
'post': { 
'properties': { 
**'title': {'type':'string', "index": "analyzed"}** 
} 
} 
} 
} 

でも、「Using Python」というクエリでヒットすることはできません! ありがとうございました!

私は、pythonバージョンelasticsearchのみをインストールします。このコードはWebのシンプルなデモコードです。

ありがとうございます!

+0

です。私のelasticsearchバージョンは2.2.1です。したがって、 "string"から "text"へのマッピングを変更することはできません。しかし、「Pythonの使用」のように、esに格納されているドキュメントと正確に一致しないクエリで検索結果を取得するにはどうすればよいですか。 – chocolate9624

答えて

0

マッピングに{"index" : "not_analyzed"}を指定すると、elasticsearchは解析せずにそのまま保存することを意味します。そのため、単に「Using Python」を検索すると結果が得られないのです。 elasticsearch 5.xでは、フィールドtypeのデータ型をtextと指定すると、elasticsearchが最初に解析して格納します。これで、あなたはあなたの質問に「Using Python」のマッチを得ることができます。 textタイプの詳細ドキュメントはhere

+0

ありがとう!タイプをテキストとして設定します。ただし、次のエラーメッセージが出力されます。{u'status ':400、u'error':{u'caused_by ':{u'reason':u '[text]フィールドの[title] u'type ':u'mapper_parsing_exception'}。 – chocolate9624

関連する問題