Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check if a field exist in an Elasticsearch document

What is the best way to check if a field of a document in elasticsearch exists? I can't find anything in the documentation.

For example if this document doesn't have the field/key "price" I don't want to return in the result.

{     "updated": "2015/09/17 11:27:27",      "name": "Eye Shadow",      "format": "1.5 g / 0.05 oz", } 

What I can do?

like image 360
Ekaitz Hernandez Troyas Avatar asked Oct 05 '15 13:10

Ekaitz Hernandez Troyas


People also ask

How do I check my Elasticsearch data?

You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .

How do I get the field name in Elasticsearch?

You may use _field_names field. The _field_names field indexes the names of every field in a document that contains any value other than null. Yes, you may only query for the existence of a field but can't aggregate on the same. The get mappings approach will not work if you are using the join functionality.

How do I view an Elasticsearch document?

You can view the document in two ways. The Table view displays the document fields row-by-row. The JSON (JavaScript Object Notation) view allows you to look at how Elasticsearch returns the document. The link is valid for the time the document is available in Elasticsearch.

How do I search for 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 .


Video Answer


1 Answers

You can use the exists filter combined with a bool/must filter like this:

{   "query": {     "filtered": {       "filter": {         "bool": {           "must": [             {               "exists": {                 "field": "price"               }             },             ...     <-- your other constraints, if any           ]         }       }     }   } } 

DEPRECATED (since ES5) You can also use the missing filter combined with a bool/must_not filter:

{   "query": {     "filtered": {       "filter": {         "bool": {           "must_not": [             {               "missing": {                 "field": "price"               }             }           ]         }       }     }   } } 
like image 189
Val Avatar answered Oct 01 '22 23:10

Val