[英]Elasticsearch Percolator with python api
嗨,我正在嘗試使用“ 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”也用於代替索引。 請幫忙。
以下文本對我有用(在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}}
字詞查詢不會標記或分析搜索文本。 因此,給出一個短語,將進行詞條查詢以查找令牌的完全匹配。 哪個不存在,所以如果您使用match查詢,它應該可以工作
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的答案,以便與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}
唯一的區別是創建索引和注冊查詢的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.