1

Hi I am trying to do a percolator index using "elasticsearch.py" api. But I am not even getting any results.

The API documentation seems to have 3 or 4 functions related to percolation.

I have checked the following possibilities. can anyone be of some help , so that I can solve it.

es = Elasticsearch()
query = {'query': {'term': {'message': 'bonsai tree'}}}
es.create(index='test', doc_type='message', percolate=query, id='kuku2', body = {"message":"bonsai tree"})
doc = {'doc': {'message': 'I am a bonsai tree'}}
k = es.percolate(index='test', doc_type='type1', body=doc)
print k

###### Result #####

 u'matches': [], u'total': 0, u'took': 0, u'_shards': {u'successful': 1, u'failed': 0, u'total': 1}}

I hope "es.percolate" is used for searching. "es.create" allows us to register documents as percolate index. But it is not so perfectly mentioned in the documentation. ".percolate" have also used in the place of index. Please help.

user3116355
  • 1,187
  • 3
  • 14
  • 17

3 Answers3

2

The following piece of text works for me (on ES 1.4.4). The key point seems to be the use of doc_type='.percolator' in es.create.

    from elasticsearch import Elasticsearch
    from elasticsearch.client.indices import IndicesClient

    es = Elasticsearch()
    ies = IndicesClient(es)

    mapping = {
      "mappings": {
        "my-type": {
          "properties": {
            "content": {
              "type": "string"
            }
          }
        }
      }
    }
    ies.create(index='test_index', body=mapping)

    query = {
        "query": {
            "match": {
                "content": "python"
            }
        }
    }
    es.create(index='test_index', doc_type='.percolator', body=query, id='python')

    doc1 = {'doc': {'content': 'this is something about python'}}
    res = es.percolate("test_index", doc_type="my-type", body = doc1)
    print res

    # result:
    # {u'matches': [{u'_id': u'python', u'_index': u'test_index'}], u'total': 1, u'took': 3, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}}


    doc2 = {'doc': {'content': 'this is another piece of text'}}
    res = es.percolate("test_index", doc_type="my-type", body = doc2)
    print res
    # result:
    # {u'matches': [], u'total': 0, u'took': 2, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}}
Roy2012
  • 11,755
  • 2
  • 22
  • 35
0

A term query wont tokenize or analyze the search text. Hence giving a phrase there will make term query to look for exact match of the token. Which doesnt exist.So if you use the match query , it should work

es = Elasticsearch()
query = {'query': {'match': {'message': 'bonsai tree'}}}
es.create(index='test', doc_type='message', percolate=query, id='kuku2', body = {"message":"bonsai tree"})
doc = {'doc': {'message': 'I am a bonsai tree'}}
k = es.percolate(index='test', doc_type='type1', body=doc)
print k
Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77
  • Hi Vineeth. i have already tried it. Now also I tried it. The result remains the same as I had earlier. No succesful match found. I tried with curl as in the documentation page. That is also not working. – user3116355 Feb 04 '15 at 08:44
0

I tweaked @Roy2012's answer a bit to use with ES 5.1

Here is my code:

import pprint
from elasticsearch import Elasticsearch
# Use your elasticsearch user, password, and host below
es = Elasticsearch(['http://user:password@host:9200/'])
mapping = {
    "mappings": {
        "doctype": {
            "properties": {
                "comment": {
                    "type": "text"
                }
            }
        },
        "queries": {
            "properties": {
                "query": {
                    "type": "percolator"
                }
            }
        }
    }
}

es.indices.create(index='comment_percolators', body=mapping, ignore=400)
word = "python"
query = {
    "query": {
        "match": {
            "comment": word
        }
    }
}
res = es.index(index='comment_percolators', doc_type='queries', body=query, id=word)
pprint.pprint(res)


doc1 = {'doc': {'comment': 'this is something about python'}}
res = es.percolate(index="comment_percolators", doc_type="doctype", body=doc1)
pprint.pprint(res)
# {u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
#  u'matches': [{u'_id': u'python', u'_index': u'comment_percolators'}],
#  u'took': 16,
#  u'total': 2}

doc2 = {'doc': {'comment': 'this is another piece of text'}}
res = es.percolate(index="comment_percolators", doc_type="doctype", body=doc2)
pprint.pprint(res)
# {u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
#  u'matches': [],
#  u'took': 23,
#  u'total': 0}

The only difference is how you create the index and register your query.

Benz
  • 140
  • 8