我是Elasticsearch(5.5.2)的新手,并且正在尝试调整查询(within this python package)以提供更符合我的需求的结果。在软件包中,我在python中具有以下功能:
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"}}
r = Q("match", feature_code='PPL')
res = self.conn.query(q).query(r)[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
我想基于 feature_code 过滤从ES(在else语句的第一部分中)获得的结果。但是,我不会像现在的两个搜索那样对一个feature_code进行过滤,而是将结果来自一组受限制的feature_codes。
我尝试使用:
r = Q("match", feature_code=['PPL','PPLA','PPLA2'])
,但这是不正确的。
我看过this help page以及at this discussion。但是仍然无法弄清楚如何同时使用 multi_match 和过滤器。困难的部分原因是我对ES缺乏经验,部分原因是我不完全了解self.conn.query(q).query(r)
语句中的第一个查询如何与第二个查询交互。
此外,我相信我的文档/数据库中可能还有其他可能与我想使用的与feature_code类似的相关字段/条件。 (特别是,我认为many of these fields应该可以使用。)如何从ES文档/数据库中获取其他字段的确切语法?