有什么方法可以在文档中添加字段但将其隐藏在_source中,还应该分析和搜索文档

Is there any way to add the field in document but hide it from _source, also document should be analysed and searchable

我想在文档中添加一个应该可搜索的字段,但是当我们这样做时 get/search 它不应该出现在 _source 下。

我尝试了索引和存储选项,但无法通过它实现。 它更像是 _all 或 copy_to,但在我的例子中,值是由我提供的(不是从文档的其他字段收集的。)

我正在寻找可以实现以下情况的映射。

当我放置文件时:

PUT my_index/_doc/1
{
  "title":   "Some short title",
  "date":    "2015-01-01",
  "content": "A very long content field..."
}

并进行搜索

获取my_index/_search

输出应该是

{
    "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "Some short title",
          "date" : "2015-01-01"
        }
      }
    ]
  }
}

还有当我进行以下搜索时

GET my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "long content"
    }
  }
}

结果应该是我

"hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "title" : "Some short title",
          "date" : "2015-01-01"
        }
      }
    ]
  }

只需使用source filtering排除content字段:

GET my_index/_search
{
  "_source": {
    "excludes": [ "content" ]
  },
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "long content"
    }
  }
}

我们可以使用下面的映射来实现:

PUT my_index
{
  "mappings": {

    "_doc": {
      "_source": {
        "excludes": [
          "content"
        ]
      },
      "properties": {
        "title": {
          "type": "text",
          "store": true 
        },
        "date": {
          "type": "date",
          "store": true 
        },
        "content": {
          "type": "text"
        }
      }
    }
  }
}

添加文档:

PUT my_index/_doc/1
{
  "title":   "Some short title",
  "date":    "2015-01-01",
  "content": "A very long content field..."
}

当您 运行 查询要在字段 'content' 上搜索内容时:

GET my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "long content"
    }
  }
}

您将获得如下点击结果:

"hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "date" : "2015-01-01",
          "title" : "Some short title"
        }
      }
    ]
  }

它隐藏字段 'content'。 :)

因此借助映射实现了它。您无需在每次 get/search 调用时都将其从查询中排除。

更多阅读源代码: https://www.elastic.co/guide/en/elasticsearch/reference/6.6/mapping-source-field.html