如何使用pyes将普通查询发送到elasticsearch

时间:2014-04-14 16:21:40

标签: python elasticsearch pyes pyelasticsearch

我有一个简单的查询,我想通过pyes发送到Elasticsearch而不使用pyes的内置方法。查询在“CURLed”时有效。

下面是我的代码,但我无法使其正常工作。当我遍历结果对象

时,它会返回此错误
Traceback (most recent call last):
  File "testQualifier.py", line 9, in <module>
    for r in results:
  File "/usr/local/lib/python2.7/dist-packages/pyes/es.py", line 1384, in __next__
    self._do_search()
  File "/usr/local/lib/python2.7/dist-packages/pyes/es.py", line 1212, in _do_search
    self._results = self._search_raw(self.start, self.chuck_size)
  File "/usr/local/lib/python2.7/dist-packages/pyes/es.py", line 1428, in _search_raw
    doc_types=self.doc_types, headers=self.headers, **query_params)
  File "/usr/local/lib/python2.7/dist-packages/pyes/es.py", line 931, in search_raw
    return self._send_request('GET', path, body, params=query_params, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/pyes/es.py", line 419, in _send_request
    raise_if_error(response.status, decoded)
  File "/usr/local/lib/python2.7/dist-packages/pyes/convert_errors.py", line 94, in raise_if_error
    raise exceptions.ElasticSearchException(error, status, result, request)
pyes.exceptions.ElasticSearchException: QueryParsingException[[test] No query registered for [facets]]; }]

有人能指出我正确的方向吗?

#!/usr/bin/env python
#-*- coding: utf-8 -*-

from pyes import *
import json
conn = ES('127.0.0.1:9200') # Use HTTP
q = {
  "facets": {
    "terms": {
      "terms": {
        "field": "Breadcrumb",
        "size": 2,
        "order": "count",
        "exclude": []
      },
      "facet_filter": {
        "fquery": {
          "query": {
            "filtered": {
              "query": {
                "bool": {
                  "should": [
                    {
                      "query_string": {
                        "fields": [
                          "Title"
                        ],
                        "query": "solar panel"
                      }
                    }
                  ]
                }
              },
              "filter": {
                "bool": {
                  "must": [
                    {
                      "fquery": {
                        "query": {
                          "query_string": {
                            "query": "VendorName:(\"abcdedf\")"
                          }
                        },
                        "_cache": true
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}
results = conn.search(query = q)
for r in results:
    print r

1 个答案:

答案 0 :(得分:0)

发现了。这有效......

#!/usr/bin/env python
#-*- coding: utf-8 -*-

from pyes import *


conn = ES('127.0.0.1:9200') # Use HTTP

q = {
  "facets": {
    "terms": {
      "terms": {
        "field": "Breadcrumb",
        "size": 2,
        "order": "count",
        "exclude": []
      },
      "facet_filter": {
        "fquery": {
          "query": {
            "filtered": {
              "query": {
                "bool": {
                  "should": [
                    {
                      "query_string": {
                        "fields": [
                          "Title"
                        ],
                        "query": "solar panel"
                      }
                    }
                  ]
                }
              },
              "filter": {
                "bool": {
                  "must": [
                    {
                      "fquery": {
                        "query": {
                          "query_string": {
                            "query": "VendorName:(\"abcdef\")"
                          }
                        },
                        "_cache": true
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}

results = conn._send_request('GET', 'vendor/_search', q)
for r in results:
    print r