0
votes

I am trying to have a cardinality aggregation on top of query_string. I am new to Elastic search and have hit some errors.

Each document looks something like this -

{
   name: 'Jon Doe'
   customId: 'x123yz'
   prevAddressMap : {'NY':'2nd St', 'DC':'1st St', 'Chicago':'1st St' ... }
},
{ ...
},
.....

I have the query to get all the records I need -

{'query':{'query_string':{'fields':['prevAddressMap.*'],'query': '1st St'}}}

I want to count all the records with unique names. So, I tried using cardinality for it -

{
  "query": {
    "query_string": {
      "fields": [
        "prevAddressMap.*"
      ],
      "query": "1st St"
    }
  },
  "aggs": {
    "distinct_count": {
      "filter": {
        "query": {
          "query_string": {
            "fields": [
              "prevAddressMap.*"
            ],
            "query": "1st St"
          }
        }
      },
      "aggs": {
        "snapId_count": {
          "cardinality": {
            "field": "customId"
          }
        }
      }
    }
  }
}

The error I am getting is -

elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', 'no [query] registered for [query]')

Can someone help me with this?

1
Have you tried using a term aggregation ? It will give the number of documents for each termPierre-Nicolas Mougel
I tried to replace it with 'cardinality', but ended up with the same errorShrivardhan

1 Answers

0
votes

The error is due to the way you applied filter aggregation. The correct way of implementing the aggregation would be:

"aggs": {
  "distinct_count": {
    "filter": {
      "query_string": {
        "fields": [
          "prevAddressMap.*"
        ],
        "query": "1st St"
      }
    },
    "aggs": {
      "snapId_count": {
        "cardinality": {
          "field": "customid"
        }
      }
    }
  }
}

Secondly, since you have already used the query to filter the documents, you no longer require to add it to aggregation. The aggregation works in resulting documents of the query. Therefore following overall query will work:

{
  "query": {
    "query_string": {
      "fields": [
        "prevAddressMap.*"
      ],
      "query": "1st St"
    }
  },
  "aggs": {
    "distinct_count": {
      "cardinality": {
        "field": "customId"
      }
    }
  }
}