2017-01-15 92 views
0

我是elasticsearch的新手。在我的索引中有一个标题:Python与Elasticsearch一起使用 当我搜索它时,除了搜索“使用Python与Elasticsearch”之外,我总是得到零命中。 像: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 

的输出是:

{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改变映射用下面的代码:

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 

输出是

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,I更新从1.9 elasticsearch〜(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"}** 
} 
} 
} 
} 

但是,它仍然无法通过查询“使用Python”获得匹配结果! 非常感谢!

我只安装python版本elasticsearch。该代码只是来自Web的简单演示代码。

非常感谢!

+0

我的elasticsearch版本是2.2.1。因此,不允许将映射从“字符串”更改为“文本”。但是,如何使用与存储在es中的文档不完全匹配的查询来获取搜索结果,如“使用Python”。 – chocolate9624

回答

0

当您在映射中指定{"index" : "not_analyzed"}时,这意味着elasticsearch将按原样存储它,而不进行分析。这就是为什么当你搜索“使用Python”时你没有得到结果。使用elasticsearch 5.x,如果您将字段type的数据类型指定为text,那么elasticsearch将首先分析它,然后将其存储。这样你就可以在查询中获得'使用Python'的匹配。你可以找到更多的文档text类型here

+0

谢谢!我将该类型设置为文本。然而,它会输出这个错误信息:{u'status':400,u'error':{u'caused_by':{u'reason':u'No在字段[标题]上声明的类型[text] u'type':u'mapper_parsing_exception'}。 – chocolate9624

相关问题