1

我正在尝试使用自定义查询 DSL 来使用 pyes 库获取结果。我有使用命令行时可以使用的查询 DSL

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_test_index_query_dsl返回上面的查询 dsl dict)

按原样搜索给了我一个: ElasticSearchException: QueryParsingException[[test_index] No query registered for [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[Expected field name but got START_OBJECT "aggs"]; }]

除了 pyes 似乎还没有“topFoundHits”功能(我认为)这一事实之外,这些错误让我望而却步。

任何想法为什么会发生这种情况以及如何解决它?太感谢了!

4

1 回答 1

1

我使用这个库完成了这项工作,您可以在其中使用常规查询 dsl JSON 语法:http ://elasticsearch-dsl.readthedocs.org/en/latest/ 。

于 2015-11-25T18:56:32.890 回答