我是elasticsearch的新手。我的索引中有一个项目,标题是:当我搜索Python和Elasticsearch时,除了搜索“使用Python和Elasticsearch”之外,我总是得到零点击。像这样:
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,我用下面的代码更改映射:
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,我将elasticsearch从1.9更新到(5,1,0,'dev')
4,我还尝试用下面的代码更改映射
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的简单演示代码。
非常感谢!
发布于 2017-01-15 07:11:48
当在映射中指定{"index" : "not_analyzed"}时,这意味着elasticsearch将按原样存储它,而不对其进行分析。这就是为什么在搜索“使用Python”时没有得到结果的原因。使用ElasticSearch5.x,如果您将字段type的数据类型指定为text,那么elasticsearch将首先分析它,然后存储它。有了它,您将能够在查询中获得“使用Python”的匹配。您可以找到关于text类型这里的更多文档。
https://stackoverflow.com/questions/41657578
复制相似问题