Elasticsearch Reference [7.10] » Query DSL » Term-level Queries » IDS

I. ID query

Each row of data, that is, documents, will have an ID. If you specify a column Field as the ID, then the column Field must be a unique key, similar to MySQL UK. Usually, for better location data, we will specify a list of fields that satisfy the UK as the ID of the document. Next, we will talk about querying by ID. Where id=?

1.1. Command line

GET /sms-logs-index/_doc/1
 

1.2. Java code

    @Test
    public void idQuery() throws IOException {
        GetRequest request = new GetRequest(index);

        GetResponse resp = client.get(request.id("1"), RequestOptions.DEFAULT);

        System.out.println(resp);
    }

Second, IDS query

MySQL > select id from (id1,id2,id3) where id in(id1,id2,id3)

2.1. Command line

POST /sms-logs-index/_search? Pretty {" query ": {" ids" : {" values ": [1, 2, 3]}}}

2.2. Java code

@Test public void idSquery () throws IOException {//1. SearchRequest object searchRequest object mySearchRequest = new SearchRequest(index); SearchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder = new SearchSourceBuilder(); builder.from(0).size(10).query(QueryBuilders.idsQuery().addIds("1", "2", "3")); mySearchRequest.source(builder); SearchResponse Search = Client.search (mySearchRequest, requestOptions. Default); GetHits () for (searchHit) getHits() for (searchHit) getHits() for (searchHit) search.getHits().getHits()) { Map<String, Object> result = hit.getSourceAsMap(); System.out.println(result); }}

Pay attention to my public number [big data], more dry goods