mindtonic
mindtonic

Reputation: 1405

Scope Elasticsearch Results to Specific Ids

I have a question about the Elasticsearch DSL.

I would like to do a full text search, but scope the searchable records to a specific array of database ids.

In SQL world, it would be the functional equivalent of WHERE id IN(1, 2, 3, 4).

I've been researching, but I find the Elasticsearch query DSL documentation a little cryptic and devoid of useful examples. Can anyone point me in the right direction?

Upvotes: 10

Views: 18527

Answers (6)

Joe - Check out my books
Joe - Check out my books

Reputation: 16943

You have two options.

  1. The ids query:
GET index/_search
{
  "query": {
    "ids": {
      "values": ["1, 2, 3"]
    }
  }
}

or

  1. The terms query:
GET index/_search
{
  "query": {
    "terms": {
      "yourNonPrimaryIdField": ["1", "2","3"]
    }
  }
}

The ids query targets the document's internal _id field (= the primary ID). But it often happens that documents contain secondary (and more) IDs which you'd target thru the terms query.

Note that if your secondary IDs contain uppercase chars and you don't set their field's mapping to keyword, they'll be normalized (and lowercased) and the terms query will appear broken because it only works with exact matches. More on this here: Only getting results when elasticsearch is case sensitive

Upvotes: 1

msayef
msayef

Reputation: 73

As discussed by Ali Beyad, ids field in the query can do that for you. Just to complement his answer, I am giving an working example. In case anyone in the future needs it.

GET index_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "field": "your query"
          }
        },
        {
          "ids" : {
            "values" : ["0aRM6ngBFlDmSSLpu_J4", "0qRM6ngBFlDmSSLpu_J4"]
          }
        }
      ]
    }
  }
}

Upvotes: 3

Moccine
Moccine

Reputation: 1031

With elasticaBundle symfony 5.2

    $query = new Query();
    $IdsQuery = new Query\Ids();
    $IdsQuery->setIds($id);
    $query->setQuery($IdsQuery);
    $this->finder->find($query, $limit);

Upvotes: 0

Sean Gao
Sean Gao

Reputation: 66

According to es doc, you can

Returns documents based on their IDs.

GET /_search
{
  "query": {
    "ids" : {
      "values" : ["1", "4", "100"]
    }
  }
}

Upvotes: 2

BrookeB
BrookeB

Reputation: 1769

Here is an example query which might work for you. This assumes that the _all field is enabled on your index (which is the default). It will do a full text search across all the fields in your index. Additionally, with the added ids filter, the query will exclude any document whose id is not in the given array.

{
  "bool": {
    "must": {
      "match": {
        "_all": "your search text"
      }
    },
    "filter": {
      "ids": {
        "values": ["1","2","3","4"]
      }
    }
  }
}

Hope this helps!

Upvotes: 11

Ali Beyad
Ali Beyad

Reputation: 11

You can create a bool query that contains an Ids query in a MUST clause: https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-ids-query.html

By using a MUST clause in a bool query, your search will be further limited by the Ids you specify. I'm assuming here by Ids you mean the _id value for your documents.

Upvotes: 1

Related Questions