[英]How can I get an hit without a exact match with es 2.X? (Python elasticsearch)
我有彈性搜索的問題。 索引中有一個項目(“'title':'使用Python與Elasticsearch'”)。 我只能通過確切的查詢得到返回的結果。 但是,當我搜索“'標題':'使用Python'時”,代碼什么都沒有。
es版本是:{u'cluster_name':u'elasticsearch',u'tagline':你知道,搜索',u'version':{u'lucene_version':u'5.4.1',你' build_hash':u'd045fc29d1932bce18b2e65ab8b297fbf6cd41a1',u'number':u'2.2.1',u'build_timestamp':u'2016-03-09T09:38:54Z',u'build_snapshot':False},u'name' :u'Lorelei Travis'}
如果我是對的,那應該是2.2.1。 代碼附后。 那么,當我使用像“使用Python”這樣的查詢進行搜索時,如何在沒有完全匹配查詢的情況下搜索命中。 謝謝!
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
使用前綴查詢? 如果你想要更高級的東西,可以考慮regexp查詢或模糊查詢。
編輯:糾正我,如果我錯了:你想要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
。
更多這里 。
編輯2:如果要匹配目標字段中任何位置包含精確查詢的所有文檔,而不僅僅是作為前綴,請使用最初建議的正則表達式查詢。 然后
{
"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
不匹配Using Python with elasticsearch
但是你說你想要精確的短語匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.