9

I'm trying to do full-text search on a mongodb db with the Elastic Search engine but I ran into a problem: no matters what search term I provide(or if I use query1 or query2), the engine always returns the same results. I think the problem is in the way I make the 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)
Rod0n
  • 1,019
  • 2
  • 14
  • 33
  • Just eyeballing this quickly... does your "fuzzy" clause need to be wrapped inside a "query"? So the structure would become "query": { "fuzzy": { ... } }, "filter": {...}. Can you post the result you are getting from your request so we can try and see if there is anything obvious. – Phil Apr 11 '13 at 13:37
  • The request returns 10 items, no matter what search_term I provide.I dont know what I'm doing wrong. – Rod0n Apr 11 '13 at 19:01
  • Is the data returned real? Are the documents valid documents, or something else? Have you tried a simple uri search that specifies the document type and search param all in one? For example: curl -XGET 'http://localhost:9200/newsidx/some_type/_search?q=art_text:hello' . Or maybe something that searches a tag that isn't your art_text field. Just trying to help you narrow things down – Phil Apr 11 '13 at 22:39

2 Answers2

20

The params parameter is not for data being sent. If you're trying to send data to the server you should specifically be using the data parameter. If you're trying to send query parameters, then you shouldn't be JSON-encoding them and just give it to params as a dict.

I suspect your first request should be the following:

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

And before someone downvotes me, yes the HTTP/1.1 spec allows data to be sent with GET requests and yes requests does support it.

Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
  • "get" works as it passes 'data=' as a url parameter in the HTTP request. If you need larger sets of data, "post" will work too, but should be saved for writing data to documents. Another benefit to "post" is it makes it harder to reproduce queries in the browser when people are probing traffic. – justdan23 Mar 21 '23 at 20:07
-2
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/

Cleb
  • 25,102
  • 20
  • 116
  • 151
alfonsoolavarria
  • 1,141
  • 11
  • 11