我对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“这样的查询进行搜索时,如果没有精确匹配的查询,我如何才能获得匹配结果呢?谢谢!
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发布于 2017-01-15 19:57:07
使用prefix查询?如果您想要更花哨的查询,可以考虑使用regexp查询或fuzzy查询。
编辑:如果我错了,请纠正我:你想让Using Python with像Using Python with Elasticsearch,Using Python with Lucene一样匹配所有结果吗?然后是像这样的映射:
request_body = {
"mappings":{
"post":{
"properties":{
"title":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
}然后是类似这样的查询:
{
"query": {
"prefix": {
"title": "Using Python with"
}
}
}应退回所有相关文件。注意,我将index字段更改为not_analyzed。
更多here。
编辑2:如果希望匹配目标字段中任意位置包含确切查询的所有文档,而不仅仅是作为前缀,请按照最初的建议使用regexp查询。然后
{
"query": {
"wildcard": {
"name": "Using Python with*"
}
}
}将像前缀查询一样工作并同时匹配Using Python with Elasticsearch和Using Python with Lucene,但是
{
"query": {
"wildcard": {
"name": "*Python with Elasticsearch"
}
}
}将仅与Using Python with Elasticsearch匹配。它不会匹配Using Python with elasticsearch,但您说您想要精确的短语匹配。
https://stackoverflow.com/questions/41660525
复制相似问题