带python api的Elasticsearch过滤器

2024-09-27 22:38:26 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我想用弹性搜索.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

结果

^{pr2}$

我希望”渗滤液“用于搜索。”es.创建“允许我们将文档注册为percolate索引。但在文件中并没有如此完美地提及。“渗滤液”也被用来代替索引。请帮忙。在


Tags: 文档pytestapitreemessageindexdoc
3条回答

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

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

我修改了@Roy2012的答案,以便与es5.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}

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

下面这段文字对我有用(在es1.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}}

相关问题 更多 >

    热门问题