Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to form a query to find all records whose id is present in some list, for example [1, 3, 10]?

How to use .nest NEST in Elasticsearch to form a query like: select * from tbl where tbl.id in [1, 3, 10] ? Or in other words, how to form a query to find all records whose id is present in some list, for example [1, 3, 10]?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use below Elasticsarch query for searching on document _id field.

POST index1/_search
{
  "query": {
    "ids": {
      "values": ["1","2","3"]
    }
  }
}

Below is equivalent .Net code.

var ids = new long[] { 1, 2, 3 };

var multiGetResponse = client.Search<MyDocument>(s => s
    .Index("index1")
    .Query(q => q
        .Ids(i => i
            .Values(ids)
        )
    )
);

If you are stroing id in seperate field in Elasticsearch document then you can use below query:

POST index1/_search
{
  "query": {
    "terms": {
      "id": ["1","2","3"]
    }
  }
}

Below is equivalent .Net code.

client.Search<MyDocument>(s => s
        .Index("index1")
        .Query(q => q      
            .Terms(t => t.Field(f => f.Name).Terms(ids))      
        )
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading