如何正确索引数据?

How to correctly index data?

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

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

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

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

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

下面应该让您大致了解映射在 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字段我们怎么指定它不被索引,这意味着我们不会在answer字段上搜索,因此分析没有意义它。

然后您可以继续以这种方式索引每对 question/answer:

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

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

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