Is it possible to get contents of copy_to field in Elasticsearch?

不羁岁月 提交于 2019-12-23 07:47:25

问题


I'm using Elasticsearch v2.3.0. Suppose I have an index with mapping:

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

So, text from subfield.subsubfield.subtext is copied to text field due to copy_to. If you query a post, then only data from text is shown in text field, because _source is not modified. If you want to obtain all text, you should aggregate all fields on client. This can be inconvenient if there are many subfields.

Is there a magic query, which allows to get text field with all text copied to it?


回答1:


In the mapping for text field set "store":"yes" The you should be able to fetch it using fields

Example:

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

Results

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


来源:https://stackoverflow.com/questions/36618549/is-it-possible-to-get-contents-of-copy-to-field-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!