Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
searching nodes on ES index
from elasticsearch import Elasticsearch
es = Elasticsearch(['localhost:9201',])
def node_phrase_query(node_id, key_phrase):
qbody = {"query":
{
"bool": {
"must": [
{"match_phrase": {"text": key_phrase}},
{"match_phrase": {"clusters": node_id}}
]
}}}
return qbody
def node_phrase_query(node_id, key_phrase):
qbody = {"query":
{
"bool": {
"must": [
{"match_phrase": {"text": key_phrase}},
{"match_phrase": {"clusters": node_id}}
]
}}}
return qbody
def node_applicant_query(node_id, applicant_name):
qbody = {"query":
{
"bool": {
"must": [
{"match": {"applicant": {"query": applicant_name}}},
{"match": {"clusters": node_id}}
]
}}}
return qbody
def any_phrase_query(phrases, nodes=None):
qbody = {"query": {"bool": {"should":[]}}}
if nodes:
qbody['query']['bool']['must'] = []
for node in nodes:
qbody['query']['bool']['must'].append({"match": {"clusters": node}})
for phrase in phrases:
qbody['query']['bool']['should'].append({"match": {"text": phrase}})
return qbody
def multi_phrase_query(phrases, node=None):
qbody = {"query": {"bool": {"must":[]}}}
if nodes:
qbody['query']['bool']['must'].append({"match": {"clusters": node}})
for phrase in phrases:
qbody['query']['bool']['must'].append({"match": {"text": phrase}})
return qbody
def search_node_phrase(n, p):
res = es.search(index='fcc_comments_part_two', body=node_phrase_query(n,p))
return res['hits']['total']
def search_node_applicant(n, a):
res = es.search(index='fcc_comments_part_two', body=node_applicant_query(n,p))
return res['hits']['total']
def search_node_any_phrase(n, ps):
res = es.search(index='fcc_comments_part_two', body=any_phrase_query(ps,n))
return res['hits']['total']
def search_node_multi_phrase(n, ps):
res = es.search(index='fcc_comments_part_two', body=multi_phrase_query(ps,n))
return res['hits']['total']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.