首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python弹性服务器:只有精确匹配才能返回结果

python弹性服务器:只有精确匹配才能返回结果
EN

Stack Overflow用户
提问于 2017-01-15 03:36:20
回答 1查看 479关注 0票数 0

我是elasticsearch的新手。我的索引中有一个项目,标题是:当我搜索Python和Elasticsearch时,除了搜索“使用Python和Elasticsearch”之外,我总是得到零点击。像这样:

1,代码

代码语言:javascript
运行
复制
    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

产出如下:

代码语言:javascript
运行
复制
{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,我用下面的代码更改映射:

代码语言:javascript
运行
复制
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

输出是

代码语言:javascript
运行
复制
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,我还尝试用下面的代码更改映射

代码语言:javascript
运行
复制
request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "not_analyzed"}**
}
}
}
}

5 --我还以这种方式改变了映射--

代码语言:javascript
运行
复制
    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}

但是,它仍然无法获得查询“使用Python”的点击量!非常感谢!!

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

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-01-15 07:11:48

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41657578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档