我对elasticsearch有问题。索引中有一个项(“'title':'Using Python with Elasticsearch'”)。我只能得到返回的结果,只有我搜索的确切查询。但是,当我搜索“'title':'Using Python with'”时,代码什么也打不到。
es版本是:{u'cluster\u name':u'elasticsearch',u'tagline':u'You Know,for Search',u'version':{u'lucene\u version':u'5.4.1',u'build\u hash':u'd045fc29d1932bce18b2e65ab8b297fbf6cd41a1',u'number':u'2.2.1',u'build\u timestamp':u'2016-03-09T09:38:54Z',u'build\u snapshot':False},u'name':u'Lorelei Travis'}
如果我是对的,应该是es2.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
使用prefix查询?如果您想要更高级的查询,可以考虑使用regexp查询或fuzzy查询。你知道吗
编辑:如果我错了请更正:您希望
Using Python with
匹配所有结果,如Using Python with Elasticsearch
,Using Python with Lucene
?然后映射如下:然后是这样的查询:
应归还所有相关文件。 注意,我将
index
字段更改为not_analyzed
。你知道吗更多here。你知道吗
编辑2: 如果要匹配包含目标字段中任何位置的精确查询的所有文档,而不只是作为前缀,请按照最初的建议使用regexp查询。那么
将像前缀查询一样工作并匹配
Using Python with Elasticsearch
和Using Python with Lucene
,但是将只匹配
Using Python with Elasticsearch
。它与Using Python with elasticsearch
不匹配,但您说过希望短语匹配。你知道吗相关问题 更多 >
编程相关推荐