Python 查询DSL在pyes搜索中不起作用
我正在尝试使用自定义查询DSL来获得使用pyes库的结果。当我使用命令行时,我有一个查询DSLPython 查询DSL在pyes搜索中不起作用,python,
elasticsearch,pyes,Python,
elasticsearch,Pyes,我正在尝试使用自定义查询DSL来获得使用pyes库的结果。当我使用命令行时,我有一个查询DSL curl -XGET localhost:9200/test_index/_search -d '{ "query": { "function_score": { "query": { "match_all": {} }, "field_value_factor": {
curl -XGET localhost:9200/test_index/_search -d '{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"field_value_factor": {
"field": "starred",
"modifier": "none",
"factor": 2
}
}
},
"aggs" : {
"types" : {
"filters" : {
"filters" : {
"category1" : { "type" : { "value" : "category1"}},
"category2" : { "type" : { "value" : "category2"}},
"category3" : { "type" : { "value" : "category3"}},
"category4": { "type" : { "value" : "category4"}},
"category5" : { "type" : { "value" : "category5"}}
}
},
"aggs": {
"topFoundHits": {
"top_hits": {
"size": 5
}
}
}
}
}
}'
这里的想法是在许多分类文档中搜索与特定字符串查询匹配的所有文档。然后使用聚合,我想按类别查找结果文档的前五名。带星号的项目被提升,以便它们显示在其他搜索结果之上
当我直接在终端中输入上面列出的命令时,这非常有效,但当我尝试将其放入pyes时,它不起作用。我不知道最好的办法是什么。pyes库的文档对于我来说真的很混乱,无法将其完全转换为pyes对象
我正在努力做到以下几点:
query_dsl = self.get_text_index_query_dsl()
resulting_docs = conn.search(query=query_dsl)
(其中self.get\u test\u index\u query\u dsl
返回上面的查询dsl dict)
按原样搜索给了我一个:
ElasticSearchException:QueryParsingException[[test_index]没有为[query]];}注册查询代码>
如果删除父“查询”映射并尝试:
query_dsl = {
"function_score": {
"query": {
"match_all": {}
},
"field_value_factor": {
"field": "starred",
"modifier": "none",
"factor": 2
}
},
"aggs" : {
"types" : {
"filters" : {
"filters" : {
"category1" : { "type" : { "value" : "category1"}},
"category2" : { "type" : { "value" : "category2"}},
"category3" : { "type" : { "value" : "category3"}},
"category4": { "type" : { "value" : "category4"}},
"category5" : { "type" : { "value" : "category5"}}
}
},
"aggs": {
"topFoundHits": {
"top_hits": {
"size": 5
}
}
}
}
}
}
这也会出现错误:ElasticSearchException:ElasticsearchParseException[预期的字段名,但得到了START_OBJECT“aggs”];}]代码>
这些错误加上pyes似乎还没有“topFoundHits”功能这一事实(我想)让我很沮丧
你知道为什么会发生这种情况以及如何解决吗?
非常感谢你 我使用这个库实现了这一点,您可以使用常规查询dsl JSON语法: