Istiak Mahmood
Istiak Mahmood

Reputation: 2422

Dealing with Null values

in elasticsearch i am trying get the values from a specific field where the field is missing or empty.

I am trying like so ---

                $query['body'] = '{
  "aggs": {
    "missing_field": {
            "missing" : {
               "field" : "category"
            }
    }
  }
}';

                $results = $client->search($query);

i get the output like ---

{
took: 4,
timed_out: false,
_shards: {},
hits: {},
aggregations: {
missing_field: {
doc_count: 1001
}
}
}

But the aggregation is returning all the documents, it is only returning the counts, but i need all the documents which are missing that fields not just the counting.

Can someone knows how to fix this problem.

Upvotes: 0

Views: 62

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52368

If you want to aggregate the results as missing/not_missing without using the php code:

{
  "query": {
    "filtered": {
      "filter": {
        "missing": {
          "field": "category"
        }
      }
    }
  }, 
  "aggs": {
    "miss": {
      "missing": {
        "field": "category"
      }
    },
    "not_miss": {
      "filter": {
        "exists": {
          "field": "category"
        }
      }
    }
  }
}

or, if you want the query to return "missing" categories, but the aggregation to count both missing and not missing categories, do this (using a global aggregation that ignores the result of the initial query):

{
  "query": {
    "filtered": {
      "filter": {
        "missing": {
          "field": "category"
        }
      }
    }
  },
  "aggs": {
    "allDocs": {
      "global": {},
      "aggs": {
        "miss": {
          "missing": {
            "field": "category"
          }
        },
        "not_miss": {
          "filter": {
            "exists": {
              "field": "category"
            }
          }
        }
      }
    }
  }
}

If you want just the missing categories, no aggregations:

{
  "query": {
    "filtered": {
      "filter": {
        "missing": {
          "field": "category"
        }
      }
    }
  }
}

You could display "some" documents within the aggregation itself, but you cannot display all. It will be very high pressure on the nodes.

Upvotes: 1

Jinksy
Jinksy

Reputation: 451

Depending on the data type of your rows in $results array/dataset you could do something like:

foreach ($results as $result) {
    //In case if $result is an object
    if (isset ($result->category) == FALSE || is_null($result->category)) {
        //Do something here as category does not exist
    }
    //In case if $result is an associative array
    if (isset ($result['category']) == FALSE || is_null($result['category'])) {
        //Do something here as category does not exist
    }
}

Upvotes: 1

Related Questions