是否有可能在Elasticsearch中获取copy_to字段的内容?

时间:2016-04-14 09:10:59

标签: elasticsearch querying

我正在使用Elasticsearch v2.3.0。假设我有一个带映射的索引:

{
"mappings": {
  "post": {
    "dynamic": false,
    "_source": {
      "enabled": true
    },
    "properties": {
      "text": {
        "norms": {
          "enabled": false
        },
        "fielddata": {
          "format": "disabled"
        },
        "analyzer": "text_analyzer",
        "type": "string"
      },
      "subfield": {
        "properties": {
          "subsubfield": {
            "properties": {
              "subtext": {
                "index": "no",
                "analyzer": "text_analyzer",
                "type": "string",
                "copy_to": "text"
              }
            }  
          }
        }
      }
    },
    "_all": {
      "enabled": false
    }
  }
}

因此,由于subfield.subsubfield.subtext,来自field的文字被复制到文字copy_to。如果您查询帖子,则text字段中仅显示来自text的数据,因为_source未被修改。如果要获取所有文本,则应聚合客户端上的所有字段。如果存在许多子场,则这可能是不方便的。

是否有魔术查询,允许获取text字段并将所有文本复制到其中?

1 个答案:

答案 0 :(得分:10)

text字段集"store":"yes"的映射中 您应该可以使用fields

获取它

示例

put test/test/_mapping
{
  "properties" : {
   "name" : { "type" : "string","copy_to": "text"},
   "text" : {"type" : "string" ,"store" :"yes"},
    "subfield": {
        "properties": {
          "subsubfield": {
            "properties": {
              "subtext": {
                "index": "no",
                "type": "string",
                "copy_to": "text"
              }
            }  
          }
        }
      }
  }   

}

put test/test/1 
{
    "name" : "hello",
    "subfield" : {
        "subsubfield" : [
            {"subtext" : "soundgarden" },
            {"subtext" : "alice in chains" },
            {"subtext" : "dio" }
        ]
    }
} 

post test/_search
{
    "fields": [
       "text"
    ]
}

<强>结果

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test",
            "_type": "test",
            "_id": "1",
            "_score": 1,
            "fields": {
               "text": [
                  "hello",
                  "soundgarden",
                  "alice in chains",
                  "dio"
               ]
            }
         }
      ]
   }
}