使用elasticsearchdsl Percolate查询的正确方法是什么?

2024-09-29 23:26:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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.').

非常感谢您对解决此问题的任何帮助


Tags: of文档testclientfield列表executesearch
1条回答
网友
1楼 · 发布于 2024-09-29 23:26:01

我将通过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

相关问题 更多 >

    热门问题