【问题标题】:How to correctly index data?如何正确索引数据?
【发布时间】:2018-04-16 09:20:00
【问题描述】:

请帮帮我。我有一个具有以下结构的 JSON 文件。

{
    "question": [ ... ], 
    "answer": [ ... ]
},
{
    ...
}

任务是以文本的形式提出请求,以便他搜索我索引的问题并以搜索结果的形式显示答案。

我想索引所有问题字段,但我不知道如何处理答案字段。

你能告诉我该做什么或至少在哪里阅读。

【问题讨论】:

标签: elasticsearch question-answering


【解决方案1】:

以下内容应该让您大致了解映射在 Elasticsearch 中的工作原理。

我首先建议您将 json 文件转换为包含此结构的多个 JSON 对象:

{
  "question":"What is the meaning of life?",
  "answer":"42."
}

你的索引看起来像这样:

PUT my_index
{
  "mappings":{
    "myFirstMapping": {
      "properties": {
        "question": {
          "type": "text",
          "analyzer": "standard"
        },
        "answer": {
          "type":"text",
          "index": "false"
        }
      }
    }
  }
}

根据文本、语言或特殊要求,您可以稍后构建更复杂的分析器,目前您只是使用标准的分析器:Standard Analyzer

看看我们如何在answer字段中指定它不包含在索引中,这意味着我们不会搜索答案字段,因此分析它没有意义。

然后您可以通过这种方式为每对问题/答案编制索引:

POST my_index/myFirstMapping
{
  "question":"What is the meaning of life?",
  "answer":"42."
}

最后,您可以执行如下搜索:

GET my_index/_search
{
  "query": {
    "match": {
      "question": "life?"
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2019-06-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多