使用Python查询ElasticSearch请求无效 [英] Querying ElasticSearch with Python Requests not working fine

查看:314
本文介绍了使用Python查询ElasticSearch请求无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用弹性搜索引擎对mongodb数据库进行全文搜索,但是我遇到了一个问题:无论我提供什么搜索字词(或者如果我使用query1或query2),引擎总是返回相同的结果。我认为问题是我提出请求的方式,但我不知道如何解决它。



这里是代码:

  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 results in results ['hits'] ['hits']]
printresults:%d%len(data)
pprint(data)


解决方案

参数参数不适用于发送数据。如果您尝试将数据发送到服务器,那么您应该具体使用data参数。如果您要发送查询参数,那么您不应该对其进行JSON编码,只能将其作为参数发送给参数。



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

  r = requests.get(uri,data = es_query)
p $ p>

在有人downvotes之前,是的,HTTP / 1.1规范允许使用GET请求发送数据,而yes请求确实支持。 >

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)

解决方案

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.

这篇关于使用Python查询ElasticSearch请求无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆