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

2

10

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?

Horrid answered 14/4, 2016 at 9:10 Comment(0)
B
15

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
{
    "stored_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"
               ]
            }
         }
      ]
   }
}
     
Bellyache answered 14/4, 2016 at 14:36 Comment(2)
With elasticsearch 7+, "store" : trueCicelycicenia
Elastic doc on the store parameter elastic.co/guide/en/elasticsearch/reference/current/…Revest
R
0

Old question, new simple test. Here's how you can test it with a simple index.

Requests

DELETE test-index
PUT test-index
{
  "mappings": {
    "properties": {
      "first_name": {
        "type": "text",
        "copy_to": "full_name" 
      },
      "last_name": {
        "type": "text",
        "copy_to": "full_name"
      },
      "full_name": {
        "type": "text",
        "store": true
      }
    }
  }
}

PUT test-index/_doc/1
{
  "first_name": "John",
  "last_name": "Doe"
}

GET test-index/_search
{
  "query": {
    "match": {
      "full_name": {
        "query": "John Doe"
      }
    }
  },
  "stored_fields": [
    "full_name"
  ]
}

Response

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "fields" : {
          "full_name" : [
            "John",
            "Doe"
          ]
        }
      }
    ]
  }
}

References

Revest answered 27/10, 2021 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.