简体   繁体   中英

How can I get an hit without a exact match with es 2.X? (Python elasticsearch)

I have a problem with elasticsearch. There is an item ("'title': 'Using Python with Elasticsearch'") in the index. I can get the returned result only I searh the exact query. However, when I searh the "'title': 'Using Python with'", the code can hit nothing.
The es version is : {u'cluster_name': u'elasticsearch', u'tagline': u'You Know, for Search', u'version': {u'lucene_version': u'5.4.1', u'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'}
If I am right, it should be es 2.2.1. The code is attached. So, how can I get the hit, when I search with the query like "Using Python with", without an exact match query. Thanks!

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

Use a prefix query? If you want something fancier, consider regexp query or fuzzy query.

EDIT: Correct me if I am wrong: you want Using Python with to match all results like Using Python with Elasticsearch , Using Python with Lucene ? Then a mapping like:

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

and then a query like:

{
    "query": {
        "prefix": {
            "title": "Using Python with"
        }
    }
}

Should return all the relevant documents. Note I changed the index field to not_analyzed .

More here .

EDIT 2: If you want to match all documents that contain the exact query anywhere in the target field, not just as a prefix, use a regexp query as suggested initially. Then

{
    "query": {
        "wildcard": {
            "name": "Using Python with*"
        }
    }
}

will work like a prefix query and match both Using Python with Elasticsearch as well as Using Python with Lucene , but

{
    "query": {
        "wildcard": {
            "name": "*Python with Elasticsearch"
        }
    }
}

will match Using Python with Elasticsearch only. It will not match Using Python with elasticsearch but you said you wanted exact phrase match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM