Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null_value mapping in Elasticsearch

I have created a mapping for a tweetb type in a twitter index:

curl -XPUT http://www.mydomain:9200/twitter/tweetb/_mapping -d '{
  "twitter": {
    "mappings": {
      "tweetb": {
        "properties": {
          "message": {
            "type": "string",
            "null_value": "NA"
          }
        }
      }
    }
  }
}'

Then, I put one document:

curl -XPUT http://www.mydomain.com:9200/twitter/tweetb/1 -d '{"message": null}'

Then, I tried to get the inserted doc back:

curl -XGET http://www.mydomain:9200/twitter/tweetb/1

And that returned:

{
  "_index": "twitter",
  "_type": "tweetb",
  "_id": "1",
  "_version": 2,
  "found" : true,
  "_source" : { "message": null }
}

I was expecting "message" : "NA" in the _source field. However, it looks like "null_value" isn't working. Am I missing something?

like image 286
Sagar Avatar asked Apr 01 '14 20:04

Sagar


People also ask

How to handle null values in Elasticsearch?

A null value cannot be indexed or searched. When a field is set to null , (or an empty array or an array of null values) it is treated as though that field has no values. Replace explicit null values with the term NULL . An empty array does not contain an explicit null , and so won't be replaced with the null_value .

What is field mapping in Elasticsearch?

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. Each document is a collection of fields, which each have their own data type. When mapping your data, you create a mapping definition, which contains a list of fields that are pertinent to the document.

What is Elasticsearch mapping type?

Elasticsearch supports two types of mappings: “Static Mapping” and “Dynamic Mapping.” We use Static Mapping to define the index and data types. However, we still need ongoing flexibility so that documents can store extra attributes.

What is term query in Elasticsearch?

Term queryedit. Returns documents that contain an exact term in a provided field. You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.


1 Answers

The "null_value" field mapping does not change the value stored, rather it changes the value that is used in searches.

If you try searching for your "message" using "NA", then it should appear in the results:

curl -XPOST http://www.mydomain.com:9200/twitter/tweetb/_search -d '{
  "query" : {
    "match" : { "message" : "NA" }
  }
}'

Of interest, it should respond with the actual value being null. Now, if you add a new document whose raw value is literally "NA" and perform the search, then you should see both results returned for the above query--one with a value and the other with null defined.

Perhaps of similar interest, this works for other queries as well based on how it is indexed, which is why a lowercase n.* matches, but N.* semi-surprisingly will not match:

curl -XPOST http://www.mydomain.com:9200/twitter/tweetb/_search -d '{
  "query" : {
    "regexp" : { "message" : "n.*" }
  }
}'
like image 160
pickypg Avatar answered Oct 05 '22 15:10

pickypg