Created
December 11, 2014 19:06
-
-
Save boblannon/a3be287c2889e0dfc631 to your computer and use it in GitHub Desktop.
searching nodes on ES index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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