This is my 17th day of The August Update Challenge

Exists query: Returns a document whose field contains an index value.

No index value exists

A field does not have an index value in the following cases:

  1. The source JSON value of the field is null or [];
  2. The length of the field value exceeds the value set in ignore_above;
  3. The value of the ignore_malformed field is not correct, but if ignore_malformed is set, the value of the field is discarded or not indexed.

Test data:

PUT /exists_test
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "index": true,
        "ignore_above": 3
      },
      "age": {
        "type": "long",
        "index": true,
        "ignore_malformed": true
      }
    }
  }
}

POST /exists_test/_doc/1
{
  "name": "beijing",
  "age": "xx"
}

POST /exists_test/_doc/2
{
  "name": "",
  "age": 5
}

POST /exists_test/_doc/3
{
  "name": [],
  "age": 5
}

POST /exists_test/_doc/4
{
  "name": null,
  "age": 5
}
Copy the code

Test for null and ignore_above

Validation When the source JSON value of a field is null or [] and the length of the field exceeds the value set in ignore_above, exists does not return a document when querying this field.

GET /exists_test/_search
{
  "query":{
    "exists": {
      "field": "name"
    }
  }
}
Copy the code

Doc1 (name); doc1 (name); doc1 (name); Doc3 and doc4 have values of null and [], so exists does not return a document when querying this field.

{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : {" total ": {" value" : 1, the "base" : "eq"}, "max_score" : 1.0, "hits" : [{" _index ":" exists_test ", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : {" name ":" ", "age" : 5}}}}]Copy the code

Test data type error

There is a problem with the value of the validation field, but if ignore_malformed is set to ignore_malformed, the value of the field is not indexed.

GET /exists_test/_search
{
  "query":{
    "exists": {
      "field": "age"
    }
  }
}
Copy the code

The value of age in document 1 is the string “xx”, but the data type of age is a long integer in the mapping. Ignore_malformed is set to unindexed. So the EXISTS query does not return doc1.

{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : {" total ": {" value" : 3, "base" : "eq"}, "max_score" : 1.0, "hits" : [{" _index ":" exists_test ", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : {" name ":" ", "age" : 5}}, {" _index ":" exists_test ", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : {" name ": []," age ": 5}}, {" _index" : "exists_test", "_type" : "_doc", "_id" : "4", "_score" : 1.0, "_source" : {" name ", null, "age" : 5}}}}]Copy the code

Note: There are several special cases that have values:

  • String empty string""or"-";
  • The array contains NULL and contains additional valued data:[null, "test"];
  • User-defined null-value defined in the mapping

Method of getting documents with missing index values

When retrieving documents with missing index values, the must_NOT Boolean query and the EXISTS query are implemented in combination.

GET /exists_test/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"exists": {"field":"age"}}
      ]
    }
  }
}
Copy the code

Query documents where the age field does not have an index value: documents 2,3, and 4 have an index value because their age value is 5. But the age value for document 1 is the string “xx”, which is not a valid long integer, and there is no index value. So the following query only returns document 1.

Result returned:

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : {" total ": {" value" : 1, the "base" : "eq"}, "max_score" : 0.0, "hits" : [{" _index ":" exists_test ", "_type" : "_doc", "_id" : "1", "_score" : 0.0, "_ignored:"/" age ", "_source" : {" name ":" Beijing ", "age" : "xx"}}}}]Copy the code