ElasticSearch query with Python queries not working fine

I am trying to do a full-text search on mongodb db using Elastic Search, but I have a problem: it doesnโ€™t matter which search term I provide (or if I use query1 or query2), the engine always returns the same results. I think the problem is how I make requests, but I donโ€™t know how to solve it.

Here is the code:

def search(search_term): query1 = { "fuzzy" : { "art_text" : { "value" : search_term, "boost" : 1.0, "min_similarity" : 0.5, "prefix_length" : 0 } }, "filter": { "range" : { "published": { "from" : "20130409T000000", "to": "20130410T235959" } } } } query2 = { "match_phrase": { "art_text": search_term } } es_query = json.dumps(query1) uri = 'http://localhost:9200/newsidx/_search' r = requests.get(uri, params=es_query) results = json.loads( r.text ) data = [res['_source']['api_id'] for res in results['hits']['hits'] ] print "results: %d" % len(data) pprint(data) 
+7
source share
2 answers

The params parameter is not intended to send data. If you are trying to send data to a server, you should use the data parameter. If you are trying to send request parameters, you should not encode their JSON and just specify the parameters as a dict.

I suspect your first request should be as follows:

 r = requests.get(uri, data=es_query) 

And before someone makes me head over heels, yes, the HTTP / 1.1 specification allows you to send data using GET requests, and yes requests support it.

+16
source
 search = {'query': {'match': {'test_id':13} }, 'sort' {'date_utc':{'order':'desc'}} } data = requests.get('http://localhost:9200/newsidx/test/_search?&pretty',params = search) print data.json() 

http://docs.python-requests.org/en/latest/user/quickstart/

-2
source

All Articles