Elasticsearch Percolator与python api

时间:2015-02-04 06:35:41

标签: python elasticsearch elasticsearch-percolate

您好我正在尝试使用“elasticsearch.py​​”api进行过滤器索引。但我甚至没有得到任何结果。

API文档似乎有3或4个与渗透有关的功能。

我检查了以下可能性。任何人都可以得到一些帮助,以便我能解决它。

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

######结果#####

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

我希望“es.percolate”用于搜索。 “es.create”允许我们将文件注册为渗滤指数。但是在文档中并没有如此完美地提到它。 “.percolate”也用于索引。请帮忙。

3 个答案:

答案 0 :(得分:2)

以下文字适用于我(在ES 1.4.4上)。关键点似乎是在doc_type='.percolator'中使用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}}

答案 1 :(得分:0)

术语查询不会标记化或分析搜索文本。因此,给出一个短语将使术语查询寻找令牌的精确匹配。哪个不存在。所以如果你使用匹配查询,它应该工作

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

答案 2 :(得分:0)

我调整了@ Roy2012的答案,以便与ES 5.1一起使用

这是我的代码:

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}

唯一的区别是你如何创建索引并注册你的查询。

相关问题