Search code examples
elasticsearch

ElasticSearch: Search through all indices, and get the results with the exact query


I'm trying to search through all indices on ElasticSearch, and only show the results which are an exact match for my query. I would specify a field, but some my indices have different field names for the same thing, so it would not work very well.

I've tried this query:

GET /_search
{
  "query": {
    "query_string": {
      "query": "sigma@gmail.com",
      "fields": ["*"]
    }
  }
}

This does work for searching through all fields, but it gets everything which contains "sigma@gmail.com" rather than only getting the results which have that exact string in any of the fields. Is there any way to make the query so it only gets the results which are an exact match to the query, rather than it returning any results which contains the query?

This is a sample of the result from the above query:

{
  "took": 7941,
  "timed_out": false,
  "_shards": {
    "total": 13,
    "successful": 13,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 16.785738,
    "hits": [
      {
        "_index": "[REDACTED]",
        "_id": "g1qD0o8BspIBF6pui4tY",
        "_score": 16.785738,
        "_source": {
          "email": "sigma@gmail.com",
          "password": "[REDACTED]"
        }
      },
      {
        "_index": "[REDACTED]",
        "_id": "EQ_G2o8Bscm15FIBl7mN",
        "_score": 12.5678215,
        "_source": {
          "email": "satish1.sigma@gmail.com",
          "display_name": "[REDACTED]",
          "username": "[REDACTED]",
          "followers": 1,
          "created_at": "Wed Dec 05 08:01:45 +0000 2012"
        }
      },
      {
        "_index": "[REDACTED]",
        "_id": "k7bG048BspIBF6puMOJK",
        "_score": 10.896135,
        "_source": {
          "id": 114820652,
          "username": "[REDACTED]",
          "email": "alice.64.sigma@gmail.com",
          "password": null,
          "ip": "[REDACTED]"
        }
      }
    ]
  }
}

Solution

  • Doesn't seem like it's possible with ElasticSearch itself. The way I got around it is by checking if the query is an exact match, client-side using my React.js application.