def query_geonames(self, placename):
"""
Wrap search parameters into an elasticsearch query to the geonames index
and return results.
Parameters
---------
conn: an elasticsearch Search conn, like the one returned by `setup_es()`
placename: str
the placename text extracted by NER system
Returns
-------
out: The raw results of the elasticsearch query
"""
# first first, try for country name
if self.is_country(placename):
q = {"multi_match": {"query": placename,
"fields": ['name', 'asciiname', 'alternativenames'],
"type" : "phrase"}}
r = Q("match", feature_code='PCLI')
res = self.conn.query(q).query(r)[0:5].execute() # always 5
#self.country_exact = True
else:
# second, try for an exact phrase match
q = {"multi_match": {"query": placename,
"fields": ['name^5', 'asciiname^5', 'alternativenames'],
"type" : "phrase"}}
res = self.conn.query(q)[0:50].execute()
# if no results, use some fuzziness, but still require all terms to be present.
# Fuzzy is not allowed in "phrase" searches.
if res.hits.total == 0:
# tried wrapping this in a {"constant_score" : {"query": ... but made it worse
q = {"multi_match": {"query": placename,
"fields": ['name', 'asciiname', 'alternativenames'],
"fuzziness" : 1,
"operator": "and"},
}
#self.fuzzy = True # idea was to preserve this info as a feature, but not using state like this
res = self.conn.query(q)[0:50].execute()
es_result = utilities.structure_results(res)
return es_result
评论列表
文章目录