elasticsearch,Python,elasticsearch" /> elasticsearch,Python,elasticsearch" />

python elasticserch:只有精确匹配才能返回结果

python elasticserch:只有精确匹配才能返回结果,python,elasticsearch,Python,elasticsearch,我不熟悉elasticsearch。我的索引中有一个标题为:将Python与Elasticsearch结合使用 当我搜索它时,我总是得到零命中率,除了我准确地搜索“使用Python和Elasticsearch”。 比如: request_body = { 'mappings':{ 'post': { 'properties': { **'title': {'type':'string', "index": "analyzed"}** } } } } 1,代码 import el

我不熟悉elasticsearch。我的索引中有一个标题为:将Python与Elasticsearch结合使用 当我搜索它时,我总是得到零命中率,除了我准确地搜索“使用Python和Elasticsearch”。 比如:

    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
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
    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
输出为:

{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': {}}}
    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
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
    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
输出是

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]'}}
    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
3,我将elasticsearch从1.9更新为(5,1,0,'dev')

    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
4,我还尝试使用下面的代码更改映射

request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "not_analyzed"}**
}
}
}
}
    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
5我也以这种方式更改映射

    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
但是,它仍然无法通过查询“使用Python”获得成功! 非常感谢

    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}
我只安装python版本的elasticsearch。该代码只是来自web的简单演示代码

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

非常感谢

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

中找到更多文档,谢谢!我将类型设置为文本。但是,它会输出以下错误消息:{u'status':400,u'error':{u'caused\u by':{u'reason':u'No handler for type[text]在字段[title]上声明,u'type':u'mapper\u parsing\u exception'}。我的elasticsearch版本是2.2.1。因此将映射从“string”更改为“text”是不允许的。但是,如果查询与es中存储的文档不完全匹配,如“使用Python”,如何获得搜索结果。
    request_body = {
'mappings':{
'post': {
'properties': {
**'title': {'type':'string', "index": "analyzed"}**
}
}
}
}