【问题标题】:Elasticsearch Percolator with python api带有 python api 的 Elasticsearch Percolator
【发布时间】:2023-04-01 20:30:01
【问题描述】:

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

【问题讨论】:

  • @IMOTOV - 你能帮帮我吗......

标签: python elasticsearch elasticsearch-percolate


【解决方案1】:

以下文本适用于我(在 ES 1.4.4 上)。关键点似乎是在es.create 中使用doc_type='.percolator'

    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}}

【讨论】:

    【解决方案2】:

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

    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。我已经试过了。现在我也试过了。结果和我之前的一样。未找到成功的匹配项。我在文档页面中尝试了 curl 。这也行不通。
    【解决方案3】:

    我稍微调整了@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}
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-10
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      相关资源
      最近更新 更多