【问题标题】:What is the correct way to use the elasticsearch-dsl Percolate query?使用 elasticsearch-dsl Percolate 查询的正确方法是什么?
【发布时间】:2020-02-10 22:38:21
【问题描述】:
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.').

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

【问题讨论】:

  • 请分享您发送给查询的文档列表示例。
  • @KevinQuinzel 列表是弹性文档列表,所以是字典列表。

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


【解决方案1】:

我将开始通过 API 解释这一点。

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

当使用 Percolate 字段创建索引时,您可以像这样指定映射:

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

这表明字段message 将是用于渗透查询的字段。

如果您想匹配文档列表,应发送包含此字段的术语列表,如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

【讨论】:

  • 我可以用 ES API 做到这一点没问题,但问题是重新创建这个确切的函数,但使用 Python elasticsearch-dsl 库。
猜你喜欢
  • 1970-01-01
  • 2019-12-17
  • 2022-09-27
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
相关资源
最近更新 更多