Python使用反斜杠查询ElasticSearch路径

时间:2016-03-03 09:26:27

标签: python-2.7 ubuntu elasticsearch-py

我使用Python的Elastic搜索扩展程序,尝试查询特定路径。

这是我的包装查询:

{
"size": 1000,
 "query": {
   "filtered": {
     "filter": {
       "bool": {
         "must": [
           {
             "term": {
               "Path": "c:\\myfolder\\myfile.txt"
             }
           }
         ]
       }
     }
   }
 }
}

kopf 插件中可以正常使用。

这是我的Python代码:

from elasticsearch import Elasticsearch
es = Elasticsearch(hosts=['my_server'])
index = "my_index"
query = '{"size":1000,"query":{"filtered":{"filter":{"bool":{"must":[{"term":{"Path":"c:\\myfolder\\myfile.txt"}}]}}}}}'
response = es.search(index=index, body=query)

由于某种原因,我收到了这个错误(没有反斜杠就不会发生):

  

/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py" ;,   第69行,在_wrapped中       return func(* args,params = params,** kwargs)File" /usr/local/lib/python2.7/dist-packages/elasticsearch/client/ init .py&#34 ;,   第530行,在搜索中       doc_type,' _search'),params = params,body = body)File" /usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py",   第329行,在perform_request中       status,headers,data = connection.perform_request(method,url,params,body,ignore = ignore,timeout = timeout)文件   " /usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.py" ;,   第106行,在perform_request中       self._raise_error(response.status,raw_data)File" /usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py",   第105行,在_raise_error中       提出HTTP_EXCEPTIONS.get(status_code,TransportError)(status_code,error_message,additional_info)   elasticsearch.exceptions.RequestError

仅当存在反斜杠时才会出现此问题。

注意:我正在使用Ubuntu。

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试更改"路径"到c:\\\\myfolder\\\\myfile.txt