使用Python查询ElasticSearch请求无效 [英] Querying ElasticSearch with Python Requests not working fine
问题描述
这里是代码:
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屋!