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

时间:2019-03-25 13:47:57

标签: elasticsearch elasticsearch-6 elasticsearch-mapping

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

2 个答案:

答案 0 :(得分:1)

只需使用source filtering来排除let doSomethingResolveCallback; let doSomethingRejectCallback; function doSomething() { return new Promise(resolve, reject) => { doSomethingResolveCallback = resolve; doSomethingRejectCallback = reject; targetiFrame.postMessage('actionA'); } } // handles messages from the iFrame function handleMessage(event) { if (event.action === "answerToActionA") { doSomethingResolveCallback(event.data); } } 字段:

content

答案 1 :(得分: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