Calle
Calle

Reputation: 1800

ElasticSearch null_value with an object?

Is it possible to use an object for the null_value mapping in ElasticSearch?

{
   "properties-v3": {
      "mappings": {
         "property": {
            "properties": {
               // ... omitting lots of properties for brevity
               "stats": { // <-- Need to populate this if null
                  "properties": {
                     "bookings": {
                        "type": "long",
                        "null_value": 0
                     },
                     "enquiries": {
                        "type": "long",
                        "null_value": 0
                     },
                     "favorites": {
                        "type": "long",
                        "null_value": 0
                     },
                     "pageViews": {
                        "type": "long",
                        "null_value": 0
                     }
                  }
               },
               // ... more properties here
            }
         }
      }
   }
}

If the stats property in the mapping above is null, I'd like to create an object with the properties populated as per their null_value mappings. Specifying null_value on the underlying properties does not seem to work.

Will ElasticSearch allow for null_value mapping for objects?

Upvotes: 4

Views: 2325

Answers (2)

Thomas Decaux
Thomas Decaux

Reputation: 22711

No you cant use null_value with type : object

But field_value_factor has an option missing check on the doc https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

Value used if the document doesn’t have that field. The modifier and factor are still applied to it as though it were read from the document.

Upvotes: 1

Vineeth Mohan
Vineeth Mohan

Reputation: 19283

First , lets see what null_value does. For a field , name , if there is null_value is set to some value , it wont add this information to _source , but rather this value will go to the reverse index. This means that you can search based on the null_value , but it wont be shown in _source.

To spot an example , lets say i am indexing the following document -

{
  "class" : "10m
  "address" : "abcs"
}

Here , lets say , for the field name we have enabled null_value as "John" Now if i execute the following query -

{
   "query" : {
             "match" : {
                   "name" : "john"
              }
   }
}

It will match the previous document even if it don't have a field name with value john. But then in the document returned this value wont be shown. This means that _source is not changed in anyway , just that we are changing the search-ability behavior of the entire context.

Now coming back to your question - if you want to put null_value to a object say , for the field name , put the following object as null_value

{
   "first_name" : "john",
   "last_name" : "dep"
}

Then , what is possible would be to assign null_value to both fields name.first_name and name.last_name as john and dep respectively.

Upvotes: 2

Related Questions