1

我想在文档中添加一个应该可以搜索的字段,但是当我们进行获取/搜索时,它不应该出现在 _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"
        }
      }
    ]
  }
4

2 回答 2

1

只需使用源过滤来排除该content字段:

GET my_index/_search
{
  "_source": {
    "excludes": [ "content" ]
  },
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "long content"
    }
  }
}
于 2019-03-25T13:50:21.470 回答
1

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

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..."
}

当您运行查询以搜索“内容”字段上的内容时:

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"
        }
      }
    ]
  }

它隐藏了“内容”字段。:)

因此在映射的帮助下实现了它。每次进行 get/search 调用时,您都不需要将其从查询中排除。

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

于 2019-03-29T11:34:11.720 回答