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)
4

2 回答 2

20

params参数不适用于正在发送的数据。如果您尝试将数据发送到服务器,您应该专门使用 data 参数。如果您尝试发送查询参数,那么您不应该对它们进行 JSON 编码,而只是将其作为 dict 提供给 params。

我怀疑您的第一个请求应该如下:

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

在有人反对我之前,是的,HTTP/1.1 规范允许使用 GET 请求发送数据,是的,请求确实支持它。

于 2013-04-12T23:44:46.627 回答
-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/

于 2016-01-08T14:01:40.573 回答