Elasticsearch Query DSL and Java API
Elasticsearch Java API
Since 5.6.x
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.10.1</version>
</dependency>
5.6.x~7.17.x, Deprecated
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.13</version>
</dependency>
5.0.x~7.17.x, Deprecated
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.17.13</version>
</dependency>
Since 7.15.x
depends on Low Level REST Client
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.10.0</version>
</dependency>
Query
Basic query
Query DSL
{ |
Java Low Level REST Client
Response performRequest(String method, String endpoint, Map<String, String> params, HttpEntity entity, Header... headers) |
Java High Level REST Client
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
Java API Client
// When your index contains semi-structured data or if you don’t have a domain object definition, you can also read the document as raw JSON data. You can use Jackson’s ObjectNode or any JSON representation that can be deserialized by the JSON mapper associated to the ElasticsearchClient. |
Specify query fields
Query DSL
{ |
Java High Level REST Client
searchSourceBuilder.fetchSource(new String[]{"author", "host"}, null); |
Query by id
Query DSL
GET /my_index/{document_id} |
Java High Level REST Client
GetRequest getRequest = new GetRequest(indexName).id(id); |
Query by ids
Query DSL
GET /my_index/_search |
Conditions
wildcard
Query DSL
{ |
Java High Level REST Client
WildcardQueryBuilder ipRegionQuery = QueryBuilders.wildcardQuery("ip_region", "*山东*"); |
Logical Operation
must/must_not
{ |
Java High Level REST Client
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); |
should
Query DSL
{ |
Java High Level REST Client
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); |
Aggregation
terms
Query DSL
{ |
Java High Level REST Client
searchSourceBuilder.aggregation( |
Scroll Search
Elasticsearch no longer recommend using the scroll API for deep pagination. If you need to preserve the index state while paging through more than 10,000 hits, use the search_after
parameter with a point in time (PIT).
In order to use scrolling, the initial search request should specify the scroll
parameter in the query string, which tells Elasticsearch how long it should keep the “search context” alive (see Keeping the search context alive), eg ?scroll=1m
.
The size
parameter allows you to configure the maximum number of hits to be returned with each batch of results. Each call to the scroll
API returns the next batch of results until there are no more results left to return, ie the hits
array is empty.
POST /my-index-000001/_search?scroll=1m |
The result from the above request includes a _scroll_id
, which should be passed to the scroll
API in order to retrieve the next batch of results.
POST /_search/scroll |
Update
Update by document ID
Using doc
- to update multiple fields at once
POST /my_index/_doc/{document_id}/_update |
Using script
POST /my_index/_doc/{document_id}/_update |
Java High Level REST Client
// Create an instance of the UpdateRequest class |
Update by document ids
Query DSL
POST /your-index/_update_by_query |
Java High Level REST Client
for (String id : ids) { |
Update by query
Query DSL
POST /your-index/_update_by_query |