Visp
Visp

Reputation: 1

ElasticSearch - Exclude hit with metadata

I'm using ES 6.6 and I'm doing a search for documents that are older than the current date. There are only 2 documents, but I get 3 items returned. The 2 existing documents and the third, are the settings and mappings. I only want to get the two documents.

I tried to add a filter with "exists", but then ES not return any document:

GET _search
{
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "products"
          }
        },
        {
          "range": {
            "happening_at": {
              "gte": "now"
            }
          }
        }
      ]
    }
  }
}

When I search only with the range, I receive the 2 correct documents, but with extra "hit" without document, only with settings and mappings.

Upvotes: 0

Views: 230

Answers (2)

Sandeep Kanabar
Sandeep Kanabar

Reputation: 1302

Welcome to SO, Adrián.

You are firing a _search across all indices since you've not specified any index name. Please try GET <your_index_name>/_search { ... request body ...}.

Also, "gte": "now" will hardly return any records since it means date greater than or equal to current date. In your case, you want records older than current date. So you could use lt:now or better still lt:now/d since now/d is good in terms of performance and allows caching.

Try the below:

GET <your_index_name>/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "products"
          }
        },
        {
          "range": {
            "happening_at": {
              "lt": "now/d"
            }
          }
        }
      ]
    }
  }
}

Upvotes: 1

LeBigCat
LeBigCat

Reputation: 1770

You have to POST your query :). If you want make a get please dont forget the /.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html

Upvotes: 0

Related Questions