1

您好我正在尝试使用“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”也用于代替索引。请帮忙。

4

3 回答 3

2

以下文本对我有用(在 ES 1.4.4 上)。关键点似乎是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}}
于 2015-12-05T18:12:02.013 回答
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
于 2015-02-04T08:14:47.013 回答
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}

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

于 2017-01-30T02:05:50.863 回答