Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch make field an array

How do I define a field in elasticsearch to always return an array.

I have a field KEYWORDS in elasticsearch that sometimes have one keyword

Elasticsearch then returns that field as a string rather than a list, this breaks the deserializer as it is expecting a list rather than a string.

This is how I defined keyword mapping:

"KEYWORDS": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "text"

    }
  }
},
like image 844
Jack Avatar asked Oct 30 '25 21:10

Jack


1 Answers

What you are trying to achieve should be handled while indexing a document. Since there is no such specific data type for array. So for e.g. if you want to have a field that store/return array of integers all you need is to define the field as type integer and while indexing always make sure that value against that field is an array even if the value is single.

So,

PUT test
{
  "mappings": {
    "_doc": {
      "properties": {
        "intArray": {
          "type": "integer"
        }
      }
    }
  }
}

PUT test/_doc/1
{
   "intArray": [10, 12, 50]
}

PUT test/_doc/1
{
   "intArray": [7]
}

Same goes for any other data type as well.

like image 74
Nishant Avatar answered Nov 02 '25 21:11

Nishant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!