使用elasticsearch-dsl Percolate查询的正确方法是什么?

时间:2020-02-10 22:38:21

标签: python elasticsearch elasticsearch-dsl elasticsearch-py elasticsearch-dsl-py

s = Search(index='test-index').using(client)
q = Q('percolate',
            field="query",
            documents=list_of_documents)
s = s.query(q)
p = s.execute()

我正在尝试对包含文档列表的索引进行渗滤查询,但出现错误

RequestError(400, 'search_phase_execution_exception', 'Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters.').

非常感谢您解决此问题。

1 个答案:

答案 0 :(得分:0)

我将开始通过API对此进行解释。

Percolate query可用于匹配存储在索引中的查询。

在使用Percolate字段创建索引时,您可以指定一个映射,如下所示:

PUT /my-index
{
    "mappings": {
         "properties": {
             "message": {
                 "type": "text"
             },
             "query": {
                 "type": "percolator"
             }
        }
    }
}

这表示字段message将用于Percolate查询。

如果您要匹配文档列表,则应像在example found in the docs中一样发送带有此字段的术语列表:

GET /my-index/_search
{
    "query" : {
        "percolate" : {
            "field" : "query",
            "documents" : [ 
                {
                    "message" : "bonsai tree"
                },
                {
                    "message" : "new tree"
                },
                {
                    "message" : "the office"
                },
                {
                    "message" : "office tree"
                }
            ]
        }
    }
}

对此进行说明,您应该:

  1. 在ES索引中设置适当的映射以渗透特定字段。

  2. 在DSL中,仅使用“ Percolated”字段发送参数列表,而不发送整个ES文档。

希望这对您有帮助:D