2

我试图从使用 python 运行的 Elastic 7.3 Percolate Documentation 中获取示例,但我的文档没有遇到任何查询。这是查看示例页面的链接:https ://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html

我使用了 python pip 包 elasticsearch。我的结果是:

{'took': 1, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}}

索引包含一个文档(渗透查询): curl -X GET "localhost:9200/inverse-index/_search"

{"took":112,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"inverse-index","_type":"_doc","_id":"1","_score":1.0,"_source":{"query":{"match":{"message":"bonsai tree"}}}}]}}

提前感谢您(请参阅下面的代码)

from elasticsearch import Elasticsearch

elasticHost = "localhost"
elasticPort = "9200"
elasticIndex = "inverse-index"

if __name__ == "__main__":

    es = Elasticsearch(elasticHost + ":" + elasticPort)
    if es.indices.exists(elasticIndex):
        print("deleting '%s' index..." % elasticIndex)
        res = es.indices.delete(index=elasticIndex)
        print(" response: '%s'" % res)

    request_body = {
        "mappings": {
            "properties": {
                "message": {
                    "type": "text"
                },
                "query": {
                    "type": "percolator"
                }
            }
        }
    }

    print("creating '%s' index..." % elasticIndex)
    print(" response: '%s'" % (es.indices.create(index=elasticIndex, body=request_body)))

    res = es.create(index=elasticIndex, id=1, body={
        "query" : {
            "match" : {
                "message" : "bonsai tree"
            }
        }
    })

    doc = {
        "query": {
            "percolate": {
                "field": "query",
                "document": {
                    "message": "A new bonsai tree in the office"
                }
            }
        }
    }

    res = es.search(index=elasticIndex, body=doc)
    print(str(res))

日志信息:

send: b'GET /inverse-index/_search HTTP/1.1\r\nHost: localhost:9200\r\nAccept-Encoding: identity\r\nContent-Length: 78\r\nconnection: keep-alive\r\ncontent-type: application/json\r\n\r\n'
send: b'{"query":{"percolate":{"field":"query","document":{"message":"bonsai tree"}}}}'
DEBUG:urllib3.connectionpool:http://localhost:9200 "GET /inverse-index/_search HTTP/1.1" 200 160
INFO:elasticsearch:GET http://localhost:9200/inverse-index/_search [status:200 request:0.008s]
DEBUG:elasticsearch:> {"query":{"percolate":{"field":"query","document":{"message":"bonsai tree"}}}}
DEBUG:elasticsearch:< {"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"max_score":null,"hits":[]}}
reply: 'HTTP/1.1 200 OK\r\n'

示例页面的代码可以很好地与 bash curl 命令配合使用。问题可能出在 python 站点上。

4

1 回答 1

0

结案。弹性索引是在后台创建的,我只需要等待几秒钟。不知道是否有一个选项可以证明,如果创建已经完成,但它对我有用。

于 2019-08-12T10:44:30.120 回答