首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在没有与es 2.X完全匹配的情况下,如何才能获得匹配?(Python elasticsearch)

在没有与es 2.X完全匹配的情况下,如何才能获得匹配?(Python elasticsearch)
EN

Stack Overflow用户
提问于 2017-01-15 19:22:08
回答 1查看 151关注 0票数 1

我对elasticsearch有个问题。索引中有一项("'title':'Using Python with Elasticsearch'")。我只能搜索精确的查询才能得到返回的结果。但是,当我搜索"'title':'Using Python with'“时,代码什么都不会找到。

U‘’build_snapshot‘:False},u’‘name’:U‘’Lorelei‘}

如果我是对的,应该是es 2.2.1。代码附在附件中。那么,当我使用"Using Python with“这样的查询进行搜索时,如果没有精确匹配的查询,我如何才能获得匹配结果呢?谢谢!

代码语言:javascript
复制
INDEX_NAME = 'test_11'
from elasticsearch import Elasticsearch
es = Elasticsearch()
print es.info()

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

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)
print res

es.index(index=INDEX_NAME, doc_type='post', id=1, body={
  'title': 'Using Python with Elasticsearch'
  }
)

es.indices.refresh(index=INDEX_NAME)

res = es.search(index=INDEX_NAME, body={'query': {'match': {'title': 'Using Python with Elasticsearch'}}})
#res = es.search(index=INDEX_NAME, body ={"query":{"match":{"title":"Using Python with"}}})

print '\n'
res = es.indices.get(index=INDEX_NAME)
print res
EN

回答 1

Stack Overflow用户

发布于 2017-01-15 19:57:07

使用prefix查询?如果您想要更花哨的查询,可以考虑使用regexp查询或fuzzy查询。

编辑:如果我错了,请纠正我:你想让Using Python withUsing Python with ElasticsearchUsing Python with Lucene一样匹配所有结果吗?然后是像这样的映射:

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

然后是类似这样的查询:

代码语言:javascript
复制
{
    "query": {
        "prefix": {
            "title": "Using Python with"
        }
    }
}

应退回所有相关文件。注意,我将index字段更改为not_analyzed

更多here

编辑2:如果希望匹配目标字段中任意位置包含确切查询的所有文档,而不仅仅是作为前缀,请按照最初的建议使用regexp查询。然后

代码语言:javascript
复制
{
    "query": {
        "wildcard": {
            "name": "Using Python with*"
        }
    }
}

将像前缀查询一样工作并同时匹配Using Python with ElasticsearchUsing Python with Lucene,但是

代码语言:javascript
复制
{
    "query": {
        "wildcard": {
            "name": "*Python with Elasticsearch"
        }
    }
}

将仅与Using Python with Elasticsearch匹配。它不会匹配Using Python with elasticsearch,但您说您想要精确的短语匹配。

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

https://stackoverflow.com/questions/41660525

复制
相关文章

相似问题

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