【问题标题】:django-elasticsearch-dsl Mapping for completion suggest field not workingdjango-elasticsearch-dsl 完成映射建议字段不起作用
【发布时间】:2019-09-07 09:42:49
【问题描述】:

这个问题已经让我发疯了好几天没有解决方案。

我从我的 django 模型创建一个文档,如下所示。

从 django_elasticsearch_dsl 导入字段

@registry.register_document class QuestionDocument(Document):
    complete = fields.CompletionField(attr='title')

    class Index:
        name = 'questions'

    class Django:
        model = QuestionModel
        fields = ['text', 'title']

现在我想执行这样的完成查询:

matched_questions = list(QuestionDocument.search().suggest("suggestions", word, completion={'field': 'complete'}).execute())

但我不断收到以下错误:

elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'Field [complete] is not a completion suggest field')

我认为问题在于该字段的映射未正确创建,但我不知道如何修复它。任何人都可以帮助解决这个问题,这真的让我发疯了。

更新:

我意识到在我的映射中,完整是作为文本字段创建的,我不知道为什么会发生这种情况或如何解决这个问题。这是我的映射:

{
  "questions" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "complete" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "text" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "title" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: django elasticsearch elasticsearch-dsl


    【解决方案1】:

    我也遇到过类似的问题。 您应该尝试像这样声明您的Document

    @registry.register_document 
    class QuestionDocument(Document):
        title = fields.TextField(
            fields={
                'raw': fields.TextField(analyzer='standard'),
                'suggest': fields.CompletionField(),
            }
         )
    
        class Index:
            name = 'questions'
    
        class Django:
            model = QuestionModel
            fields = ['text']
    

    然后从标题字段中恢复建议:

    from elasticsearch import Elasticsearch
    from elasticsearch_dsl import Search
    
    client = Elasticsearch("localhost:9200")
    s = Search(using=client)
    query = s.suggest('name_suggestion',"your_prefixword",completion={'field':'title.suggest'})
    response = query.execute()
    response.suggest['name_suggestion']
    

    希望对您有所帮助。让我知道这是否有效。

    【讨论】:

      【解决方案2】:

      您的索引是自动创建的,而不是您使用所需的映射创建它。您需要在索引任何文档之前创建索引。不知道这是如何在django_elasticsearch_dsl 中完成的,但在elasticsearch_dsl 中它只会调用QuestionDocument.init()

      希望这会有所帮助!

      【讨论】:

      • 我了解索引及其工作原理。我不明白为什么complete 被映射到text 字段,而不是我明确声明的completion 字段?
      • 因为您只在 python 中声明了它并且没有将该声明发送到 elasticsearch,所以 elasticsearch 只是为您生成了一个映射 - 它看到一个包含一些字符串的字段并且默认映射是 @987654327带有keyword 多字段的@字段
      猜你喜欢
      • 1970-01-01
      • 2019-09-26
      • 2015-12-01
      • 1970-01-01
      • 2019-01-15
      • 2020-08-29
      • 2017-06-28
      • 2011-09-07
      相关资源
      最近更新 更多