I tried to get the example from Elastic 7.3 Percolate Documentation run with python, but my document doesnt hit any query. Here is the link to see the example page: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html
I used python pip package elasticsearch. My result is:
{'took': 1, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}}
The index contains a document (percolated query): 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"}}}}]}}
Thank you in advance (see my code below)
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))
Log information:
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'
The code of the example page works nicely with bash curl command. The Problem might be on the python site.
Case closed. The elastic index is created in the background and i just had to wait a few seconds. Don't know if there is an option to prove, if the creation is already done, but it worked for me.