Elasticsearch 6.4基本操作 - Java版
1. Elasticsearch Java API有四类client连接方式
- TransportClient
- RestClient
- Jest
- Spring Data Elasticsearch
其中TransportClient和RestClient是Elasticsearch原生的api。TransportClient可以支持2.x,5.x版本,TransportClient将会在Elasticsearch 7.0弃用并在8.0中完成删除,替而代之,我们使用Java High Level REST Client,它使用HTTP请求而不是Java序列化请求。
Jest是Java社区开发的,是Elasticsearch的Java Http Rest客户端;Spring Data Elasticsearch是spring集成的Elasticsearch开发包。
建议:TransportClient将会在后面的版本中弃用,因此不推荐后续使用;而Jest由于是社区维护,所以更新有一定延迟,目前最新版对接ES6.3.1,近一个月只有四个issue,说明整体活跃度较低,因此也不推荐使用;Spring Data Elasticsearch主要是与Spring生态对接,可以在web系统中整合到Spring中使用。目前比较推荐使用官方的高阶、低阶Rest Client,官方维护,比较值得信赖。本文主要介绍RestClient,其他的只做简单概述。
2. TransportClient
这里考虑到后面版本将弃用TransportClinet,主要简单介绍TransportClient的创建、以及一些基本增删改查操作。
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>transport</artifactId>
- <version>6.4.3</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>6.4.3</version>
- </dependency>
- /**
- * @Author: Yang JianQiu
- * @Date: 2018/11/12 16:40
- *
- * transportClient将会在7.0版本上过时,并在8.0版本上移除掉,建议使用Java High Level REST Client
- */
- public class TransportClientUtils {
- private TransportClient client = null;
- /**
- * 这里使用饿汉单例模式创建TransportClient
- */
- public TransportClientUtils() {
- if (client == null){
- synchronized (TransportClientUtils.class){
- if (client == null){
- client = getClient();
- }
- }
- }
- }
- public TransportClient getClient(){
- TransportClient client = null;
- try {
- Settings settings = Settings.builder()
- .put("client.transport.sniff", true)
- .put("cluster.name", "bigdata").build();
- client = new PreBuiltTransportClient(settings)
- .addTransportAddress(new TransportAddress(new InetSocketAddress("192.168.187.201", 9300)));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return client;
- }
- public viod test(){
- //增,插入记录
- IndexResponse response = client.prepareIndex("twitter", "_doc")
- .setSource(json, XContentType.JSON)
- .get();
- //根据Id查询
- GetResponse response = client.prepareGet("twitter", "_doc", 1).get();
- //根据Id删除
- DeleteResponse response = client.prepareDelete("twitter", "_doc", 1).get();
- //根据Id更新对应的字段
- UpdateRequest updateRequest1 = new UpdateRequest("twitter", "_doc", "NpEWCGcBi36MQkKOSdf3")
- .doc(jsonBuilder()
- .startObject()
- .field("user", "tom")
- .endObject()
- );
- client.update(updateRequest1).get();
- //另外还有批处理API、search负责查询API、Aggregate聚合API...
- }
- }
具体的TransportClient的其他API应用可以参考Elasticsearch的TransportClient API doc:
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html
3. Rest Client
后面的elasticSearch版本将主要使用Rest Client操作数据。
Rest Client分为Java Low REST Client和Java High Level REST Client。
maven的依赖包:
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-high-level-client</artifactId>
- <version>6.4.3</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-client</artifactId>
- <version>6.4.3</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>6.4.3</version>
- </dependency>
RestHighLevelClient与RestClient的创建:
- /**
- * @author: Swordfall Yeung
- * @date:
- * @desc:
- */
- public class RestClientUtils {
- /**
- * 高阶Rest Client
- */
- private RestHighLevelClient client = null;
- /**
- * 低阶Rest Client
- */
- private RestClient restClient = null;
- /**
- * 这里使用饿汉单例模式创建RestHighLevelClient
- */
- public RestClientUtils() {
- if (client == null) {
- synchronized (RestHighLevelClient.class) {
- if (client == null) {
- client = getClient();
- }
- }
- }
- }
- private RestHighLevelClient getClient() {
- RestHighLevelClient client = null;
- try {
- client = new RestHighLevelClient(
- RestClient.builder(
- new HttpHost("192.168.187.201", 9300, "http")
- )
- );
- } catch (Exception e) {
- e.printStackTrace();
- }
- return client;
- }
- private RestClient getRestClient() {
- RestClient client = null;
- try {
- client = RestClient.builder(
- new HttpHost("192.168.187.201", 9300, "http")
- ).build();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return client;
- }
- public void closeClient() {
- try {
- if (client != null) {
- client.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * document API 主要是些简单的增删改查操作
- */
- public void documentAPI() {
- //...
- }
- /**
- * Search API 主要是些复杂查询操作
- */
- public void searchAPI() {
- //...
- }
- }
RestHighLevelAPI分为Document APIs、Search APIs、Miscellaneous APIs、Indices APIs、Cluster APIs...等等,这里主要介绍常用的Document APIs和Search APIs,其余的APIs可以参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
3.1 Document APIs
Document APIs主要涉及些增删改查等操作,包括Single document APIs单条操作和Multi-document APIs批量操作。Document APIs均可以设置可选参数,实现同步、异步,也均可抛出异常,这里以Index API为例,后面的不再简述。
Index API介绍:
- /**
- * 增,插入记录
- * 插入操作有四种方式,分同步异步操作,可选参数设置,结果返回IndexResponse,抛出异常
- * @throws Exception
- */
- public void index() throws Exception{
- //第一种方式: String
- IndexRequest request = new IndexRequest("posts", "doc", "1");
- String jsonString = "{" +
- "\"user\":\"kimchy\"," +
- "\"postDate\":\"2013-01-30\"," +
- "\"message\":\"trying out Elasticsearch\"" +
- "}";
- request.source(jsonString, XContentType.JSON);
- //第二种方式: Map
- Map<String, Object> jsonMap = new HashMap<>();
- jsonMap.put("user", "kimchy");
- jsonMap.put("postDate", new Date());
- jsonMap.put("message", "trying out Elasticsearch");
- IndexRequest indexRequest = new IndexRequest("posts", "doc", "1").source(jsonMap);
- //第三种方式: XContentBuilder automatically converted to JSON
- XContentBuilder builder = XContentFactory.jsonBuilder();
- builder.startObject();
- {
- builder.field("user", "kimchy");
- builder.timeField("postDate" , new Date());
- builder.field("message", "trying out Elasticsearch");
- }
- builder.endObject();
- IndexRequest indexRequest1 = new IndexRequest("posts", "doc", "1")
- .source(builder);
- //第四种方式: source -> key-pairs
- IndexRequest indexRequest2 = new IndexRequest("posts", "doc", "1")
- .source("user", "kimchy",
- "postDate", new Date(),
- "message", "trying out Elasticsearch"
- );
- //可选的参数设置
- request.routing("routing");
- request.parent("parent");
- request.timeout(TimeValue.timeValueSeconds(1));
- request.timeout("1s");
- request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
- request.setRefreshPolicy("wait_for");
- request.version(2);
- request.versionType(VersionType.EXTERNAL);
- request.opType(DocWriteRequest.OpType.CREATE);
- request.opType("create");
- request.setPipeline("pipeline");
- //同步执行
- IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
- //异步执行
- ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>() {
- @Override
- public void onResponse(IndexResponse indexResponse) {
- }
- @Override
- public void onFailure(Exception e) {
- }
- };
- client.indexAsync(request, RequestOptions.DEFAULT, listener);
- //Index Response
- String index = indexResponse.getIndex();
- String type = indexResponse.getType();
- String id = indexResponse.getId();
- long version = indexResponse.getVersion();
- //抛出异常
- IndexRequest request1 = new IndexRequest("posts", "doc", "1")
- .source("field", "value")
- .version(1);
- try {
- IndexResponse response = client.index(request, RequestOptions.DEFAULT);
- } catch (ElasticsearchException e) {
- if (e.status() == RestStatus.CONFLICT){
- }
- }
- }
Get API介绍
- /**
- * 根据 id 获取数据
- * @throws Exception
- */
- public void get() throws Exception{
- GetRequest request = new GetRequest("posts", "doc", "1");
- //可选参数设置
- request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
- String[] includes = new String[]{"message", "*Date"};
- String[] excludes = Strings.EMPTY_ARRAY;
- FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
- request.fetchSourceContext(fetchSourceContext);
- //同步执行
- GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
- //异步执行 listener的写法参照Index的异步执行的listener
- client.getAsync(request, RequestOptions.DEFAULT, listener);
- //Get Response 获取信息
- //抛出异常
- }
Exists API介绍
- /**
- * 是否存在
- * @throws Exception
- */
- public void exists() throws Exception{
- GetRequest getRequest = new GetRequest("posts", "doc", "1");
- getRequest.fetchSourceContext(new FetchSourceContext(false));
- getRequest.storedFields("_none_");
- //同步执行
- boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
- //异步执行 listener的写法参照Index的
- client.existsAsync(getRequest, RequestOptions.DEFAULT, listener);
- if (exists){
- System.out.println("存在");
- }else {
- System.out.println("不存在");
- }
- }
Delete API介绍
- /**
- * 根据id删除
- * @throws Exception
- */
- public void delete() throws Exception{
- DeleteRequest request = new DeleteRequest("posts", "doc", "1");
- //同步执行
- DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
- //异步执行 listener参照index的
- client.deleteAsync(request, RequestOptions.DEFAULT, listener);
- //Delete Response
- String index = deleteResponse.getIndex();
- // document was not found
- if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
- }
- //抛出异常
- }
Update API介绍
- /**
- * 根据id更新
- * @throws Exception
- */
- public void update() throws Exception{
- UpdateRequest request = new UpdateRequest("posts", "doc", "1");
- Map<String, Object> parameters = Collections.singletonMap("count", 4);
- //第一种方式:inline script
- Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.field += params.count", parameters);
- request.script(inline);
- //第二种方式:stored script
- Script stored = new Script(ScriptType.STORED, null, "increment-field", parameters);
- request.script(stored);
- //第三种方式:partial document String
- String jsonString = "{" +
- "\"updated\":\"2017-01-01\"," +
- "\"reason\":\"daily update\"" +
- "}";
- request.doc(jsonString, XContentType.JSON);
- //第四种方式:partial document Map
- Map<String, Object> jsonMap = new HashMap<>();
- jsonMap.put("updated", new Date());
- jsonMap.put("reason", "daily update");
- request.doc(jsonMap);
- //第五种方式:partial document XContentBuilder
- XContentBuilder builder = XContentFactory.jsonBuilder();
- builder.startObject();
- {
- builder.timeField("updated", new Date());
- builder.field("reason", "daily update");
- }
- builder.endObject();
- request.doc(builder);
- //第六种方式:partial document Object key-pairs
- request.doc("updated", new Date(),
- "reason", "daily update");
- //upserts
- String jsonString1 = "{\"created\":\"2017-01-01\"}";
- request.upsert(jsonString1, XContentType.JSON);
- //同步执行
- UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
- //异步执行, listener创建参考index的
- client.updateAsync(request, RequestOptions.DEFAULT, listener);
- //update Response
- GetResult result = updateResponse.getGetResult();
- if (result.isExists()) {
- String sourceAsString = result.sourceAsString();
- Map<String, Object> sourceAsMap = result.sourceAsMap();
- byte[] sourceAsBytes = result.source();
- } else {
- }
- //抛出异常
- }
Bulk API介绍
- /**
- * 批量处理
- * @throws Exception
- */
- public void bulk() throws Exception{
- BulkRequest request = new BulkRequest();
- //Other
- request.add(new DeleteRequest("posts", "doc", "3"));
- request.add(new UpdateRequest("posts", "doc", "2")
- .doc(XContentType.JSON, "other", "test"));
- request.add(new IndexRequest("posts", "doc", "4")
- .source(XContentType.JSON, "field", "baz"));
- //同步执行
- BulkResponse bulkResponses = client.bulk(request, RequestOptions.DEFAULT);
- //异步执行
- ActionListener<BulkResponse> listener = new ActionListener<BulkResponse>() {
- @Override
- public void onResponse(BulkResponse bulkResponse) {
- }
- @Override
- public void onFailure(Exception e) {
- }
- };
- client.bulkAsync(request, RequestOptions.DEFAULT, listener);
- //Bulk Response 批处理结果
- for (BulkItemResponse bulkItemResponse: bulkResponses){
- DocWriteResponse itemResponse = bulkItemResponse.getResponse();
- if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
- || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
- IndexResponse indexResponse = (IndexResponse) itemResponse;
- } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
- UpdateResponse updateResponse = (UpdateResponse) itemResponse;
- } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
- DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
- }
- }
- for (BulkItemResponse bulkItemResponse : bulkResponses) {
- if (bulkItemResponse.isFailed()) {
- BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
- }
- }
- //Bulk Processor 自定义批处理器
- BulkProcessor.Listener listener1 = new BulkProcessor.Listener() {
- @Override
- public void beforeBulk(long l, BulkRequest bulkRequest) {
- }
- @Override
- public void afterBulk(long l, BulkRequest bulkRequest, BulkResponse bulkResponse) {
- }
- @Override
- public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {
- }
- };
- BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
- (request1, bulkListener) -> client.bulkAsync(request1, RequestOptions.DEFAULT, bulkListener);
- BulkProcessor bulkProcessor = BulkProcessor.builder(bulkConsumer, listener1).build();
- BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer1 =
- (request2, bulkListener) -> client.bulkAsync(request2, RequestOptions.DEFAULT, bulkListener);
- BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer1, listener1);
- builder.setBulkActions(500);
- builder.setBulkSize(new ByteSizeValue(1L, ByteSizeUnit.MB));
- builder.setConcurrentRequests(0);
- builder.setFlushInterval(TimeValue.timeValueSeconds(10L));
- builder.setBackoffPolicy(BackoffPolicy
- .constantBackoff(TimeValue.timeValueSeconds(1L), 3));
- //Once the BulkProcessor is created requests can be added to it:
- IndexRequest one = new IndexRequest("posts", "doc", "1").
- source(XContentType.JSON, "title",
- "In which order are my Elasticsearch queries executed?");
- IndexRequest two = new IndexRequest("posts", "doc", "2")
- .source(XContentType.JSON, "title",
- "Current status and upcoming changes in Elasticsearch");
- IndexRequest three = new IndexRequest("posts", "doc", "3")
- .source(XContentType.JSON, "title",
- "The Future of Federated Search in Elasticsearch");
- bulkProcessor.add(one);
- bulkProcessor.add(two);
- bulkProcessor.add(three);
- boolean terminated = bulkProcessor.awaitClose(30L, TimeUnit.SECONDS);
- }
Multi-Get API介绍
- /**
- * 根据id批量获取数据
- * @throws Exception
- */
- public void multiGet() throws Exception{
- MultiGetRequest request = new MultiGetRequest();
- request.add(new MultiGetRequest.Item("index","type","example_id"));
- request.add(new MultiGetRequest.Item("index", "type", "another_id"));
- //optional arguments
- request.add(new MultiGetRequest.Item("index", "type", "example_id") .fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE));
- //同步执行
- MultiGetResponse responses = client.mget(request, RequestOptions.DEFAULT);
- //异步执行 listener参考Index的
- client.mgetAsync(request, RequestOptions.DEFAULT, listener);
- //Multi Get Response
- MultiGetItemResponse firstItem = response.getResponses()[0];
- GetResponse firstGet = firstItem.getResponse();
- if (firstGet.isExists()) {
- }
- }
3.2 Search APIs
- public void search() throws Exception{
- //match all query 查询所有数据
- SearchRequest searchRequest = new SearchRequest();
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.matchAllQuery());
- searchRequest.source(searchSourceBuilder);
- //使用SearchSourceBuilder查询指定字段
- SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
- sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));
- sourceBuilder.from(0);
- sourceBuilder.size(5);
- sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
- SearchRequest searchRequest2 = new SearchRequest();
- //index 数据库
- searchRequest2.indices("posts");
- searchRequest2.source(sourceBuilder);
- //Building queries
- //One way, QueryBuilder can be created using its constructor 使用QueryBuilder的构造函数
- MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "kimchy");
- matchQueryBuilder.fuzziness(Fuzziness.AUTO);
- matchQueryBuilder.prefixLength(3);
- matchQueryBuilder.maxExpansions(10);
- //Two way, QueryBuilder objects can also be created using the QueryBuilders utility class. 直接使用matchQuery
- QueryBuilder matchQueryBuilder1 = matchQuery("user", "kimchy")
- .fuzziness(Fuzziness.AUTO)
- .prefixLength(3)
- .maxExpansions(10);
- searchSourceBuilder.query(matchQueryBuilder1);
- //Specifying Sorting 指定排序
- sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
- sourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.ASC));
- //Source filtering, turn off _source retrieval completely
- sourceBuilder.fetchSource(false);
- //an array of one or more wildcard patterns to control which fields get included or excluded in a more fine grained way
- String[] includeFields = new String[] {"title", "user", "innerObject.*"};
- String[] excludeFields = new String[] {"_type"};
- sourceBuilder.fetchSource(includeFields, excludeFields);
- //Requesting Aggregations
- SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
- TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")
- .field("company.keyword");
- aggregation.subAggregation(AggregationBuilders.avg("average_age")
- .field("age"));
- searchSourceBuilder2.aggregation(aggregation);
- //Requesting Suggestions
- SearchSourceBuilder searchSourceBuilder3 = new SearchSourceBuilder();
- SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("kmichy");
- SuggestBuilder suggestBuilder = new SuggestBuilder();
- suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
- searchSourceBuilder3.suggest(suggestBuilder);
- //同步执行
- SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
- //异步执行 listener参考index的
- client.searchAsync(searchRequest, RequestOptions.DEFAULT, listener);
- //SearchResponse
- RestStatus status = searchResponse.status();
- for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
- // failures should be handled here
- }
- //Retrieving SearchHits 获取结果数据
- SearchHits hits = searchResponse.getHits();
- long totalHits = hits.getTotalHits();
- float maxScore = hits.getMaxScore();
- SearchHit[] searchHits = hits.getHits();
- for (SearchHit hit : searchHits) {
- // do something with the SearchHit
- String index = hit.getIndex();
- String type = hit.getType();
- String id = hit.getId();
- float score = hit.getScore();
- String sourceAsString = hit.getSourceAsString();
- Map<String, Object> sourceAsMap = hit.getSourceAsMap();
- String documentTitle = (String) sourceAsMap.get("title");
- List<Object> users = (List<Object>) sourceAsMap.get("user");
- Map<String, Object> innerObject =
- (Map<String, Object>) sourceAsMap.get("innerObject");
- }
- //Retrieving Aggregations
- Aggregations aggregations = searchResponse.getAggregations();
- Terms byCompanyAggregation = aggregations.get("by_company");
- Terms.Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic");
- Avg averageAge = elasticBucket.getAggregations().get("average_age");
- double avg = averageAge.getValue();
- Range range = aggregations.get("by_company");
- Map<String, Aggregation> aggregationMap = aggregations.getAsMap();
- Terms companyAggregation = (Terms) aggregationMap.get("by_company");
- List<Aggregation> aggregationList = aggregations.asList();
- for (Aggregation agg : aggregations) {
- String type = agg.getType();
- if (type.equals(TermsAggregationBuilder.NAME)) {
- Terms.Bucket elasticBucket1 = ((Terms) agg).getBucketByKey("Elastic");
- long numberOfDocs = elasticBucket1.getDocCount();
- }
- }
- //Retrieving Suggestions
- Suggest suggest = searchResponse.getSuggest();
- TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
- for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
- for (TermSuggestion.Entry.Option option : entry) {
- String suggestText = option.getText().string();
- }
- }
- }
Multi-search APIs介绍:
- public void multiSearch() throws Exception{
- MultiSearchRequest request = new MultiSearchRequest();
- SearchRequest firstSearchRequest = new SearchRequest();
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy"));
- firstSearchRequest.source(searchSourceBuilder);
- request.add(firstSearchRequest);
- SearchRequest secondSearchRequest = new SearchRequest();
- searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.matchQuery("user", "luca"));
- secondSearchRequest.source(searchSourceBuilder);
- request.add(secondSearchRequest);
- //同步执行
- MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);
- //同步执行 listener参考index的
- client.msearchAsync(request, RequestOptions.DEFAULT, listener);
- //MultiSearchResponse
- MultiSearchResponse.Item firstResponse = response.getResponses()[0];
- SearchResponse searchResponse = firstResponse.getResponse();
- MultiSearchResponse.Item secondResponse = response.getResponses()[1];
- searchResponse = secondResponse.getResponse();
- }
SearchTemplate APIs介绍:
- /**
- * 查询模板
- * @throws Exception
- */
- public void searchTemplate() throws Exception{
- SearchTemplateRequest request = new SearchTemplateRequest();
- request.setRequest(new SearchRequest("posts"));
- //Inline Templates
- request.setScriptType(ScriptType.INLINE);
- //instead of providing an inline script
- request.setScriptType(ScriptType.STORED);
- request.setScript(
- "{" +
- " \"query\": { \"match\": { \"{{ field }}\": \"{{ value }}\" } }," +
- " \"size\": \"{{ size }}\"" +
- "}");
- Map<String, Object> scriptParams = new HashMap<>();
- scriptParams.put("field", "title");
- scriptParams.put("value", "elasticsearch");
- scriptParams.put("size", 5);
- request.setScriptParams(scriptParams);
- //同步执行
- SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
- //异步执行 listener参考Index的
- client.searchTemplateAsync(request, RequestOptions.DEFAULT, listener);
- //SearchTemplate Response
- SearchResponse searchResponse = response.getResponse();
- BytesReference source = response.getSource();
- }
Multi-SearchTemplate APIs介绍:
- /**
- * 多个查询模板执行
- * @throws Exception
- */
- public void MultiSearchTemplate() throws Exception{
- String[] searchTerms = {"elasticsearch", "logstash", "kibana"};
- MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
- for (String searchTerm: searchTerms) {
- SearchTemplateRequest request = new SearchTemplateRequest();
- request.setRequest(new SearchRequest("posts"));
- request.setScriptType(ScriptType.INLINE);
- request.setScript(
- "{" +
- " \"query\": { \"match\": { \"{{field}}\": \"{{value}}\" }}," +
- " \"size\": \"{{size}}\"" +
- "}"
- );
- Map<String, Object> scriptParams = new HashMap<>();
- scriptParams.put("field", "title");
- scriptParams.put("value", searchTerm);
- scriptParams.put("size", 5);
- request.setScriptParams(scriptParams);
- multiRequest.add(request);
- }
- //同步执行
- MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
- //异步执行
- ActionListener<MultiSearchTemplateResponse> listener = new ActionListener<MultiSearchTemplateResponse>() {
- @Override
- public void onResponse(MultiSearchTemplateResponse response) {
- }
- @Override
- public void onFailure(Exception e) {
- }
- };
- client.msearchTemplateAsync(multiRequest, RequestOptions.DEFAULT, listener);
- //MultiSearchTemplateResponse
- for (MultiSearchTemplateResponse.Item item : multiResponse.getResponses()) {
- if (item.isFailure()) {
- String error = item.getFailureMessage();
- } else {
- SearchTemplateResponse searchTemplateResponse = item.getResponse();
- SearchResponse searchResponse = searchTemplateResponse.getResponse();
- searchResponse.getHits();
- }
- }
- }
其余APIs可以参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
4. Jest
Jest是第三方工具,是ElasticSearch的Java HTTP Rest客户端。Jest填补了ElasticSearch缺少Http Rest接口客户端的空白。
Jest的API为:
Jest的具体用法参考:https://blog.csdn.net/u010466329/article/details/75020956/
Jest的代码github地址:https://github.com/searchbox-io/Jest
5. Spring Data Elasticsearch
可参考https://blog.csdn.net/qq_33314107/article/details/80725994
6. 常见报错
NoNodeAvailableException报错,主要是外网IP没有配置
在elasticSearch.yml上配置transport.host和transport.tcp.port即可
transport.host: localhost
transport.tcp.port: 9300
localhost可以是具体的IP地址
7. 总结
个人编写的TransportClient和RestClient的github Demo:
https://github.com/SwordfallYeung/ElasticSearchDemo
参考资料:
https://www.elastic.co/guide/en/elasticsearch/client/index.html
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-overview.html
https://blog.csdn.net/qq_33314107/article/details/80725913
Elasticsearch 6.4基本操作 - Java版的更多相关文章
- 树的基本操作java版
看了一下树的定义,一些基本的操作,遍历,获取节点数,获取深度等等..这里参考了西电版的数据结构,基本用的都是递归实现的. 很多说明代码中都有,每个方法我都测了一下,这里我把节点类BTreeNode作为 ...
- 实现二叉树的基本操作(Java版)
近期研究了一下二叉树,试着用Java语言实现了二叉树的基本操作,下面分享一下实现代码: package com.sf.test; import java.util.ArrayDeque; import ...
- 20165234 [第二届构建之法论坛] 预培训文档(Java版) 学习总结
[第二届构建之法论坛] 预培训文档(Java版) 学习总结 我通读并学习了此文档,并且动手实践了一遍.以下是我学习过程的记录~ Part1.配置环境 配置JDK 原文中提到了2个容易被混淆的概念 JD ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
- ArcGIS Server 10 Java 版的Rest服务手动配置方法
Java版的Manager中发布的服务默认只发布了该服务的SOAP接口,而REST接口需要用户在信息服务器,如Tomcat. Apache.WebLogic等中手工配置.由于在Java版的Server ...
- PetaPojo —— JAVA版的PetaPoco
背景 由于工作的一些原因,需要从C#转成JAVA.之前PetaPoco用得真是非常舒服,在学习JAVA的过程中熟悉了一下JAVA的数据组件: MyBatis 非常流行,代码生成也很成熟,性能也很好.但 ...
- 【转载】java版打字练习软件
网上找到一个java版的打字的游戏 import java.applet.Applet; import java.applet.AudioClip; import java.awt.Dimension ...
- Elasticsearch 1.X 版本Java插件开发
接上一篇<Elasticsearch 2.X 版本Java插件开发简述> 开发1.X版本elasticsearch java插件与2.X版本有一些不同,同时在安装部署上也有些不同,主要区别 ...
- Elasticsearch 2.X 版本Java插件开发简述
1:elasticsearch插件分类简述 2:Java插件开发要点 3:如何针对不同版本elasticsearch提供多版本的插件 4:插件具有外部依赖时遇到的一些问题(2016-09-07更新) ...
随机推荐
- MySql 学习之路-基础
Mysql 自学之路 本文包含基础部分与高级部分 一.基础 数据库操作 Show databases:显示所有的数据库 Show tables: 显示所有的数据库表 Use databasename: ...
- 记录Nginx模块开发
--with-http_stub_status_module模块:Nginx的客户端状态 Syntax:stub_status Default:-- Content:server,location l ...
- 【Teradata Utility】使用SQL Assistant导出导入数据
1.导出 (1)选择菜单栏File,点击Export Results,输入导出数据的SQL: select * from etl_data.soure_table; (2)选择导出数据格式为txt或h ...
- Zabbix 3.4.7针对一些主机设置期间维护
场景说明: 由于公司有些主机设置了定时开机关机,每次开机关机得时候都会发邮件告警,每次都需要值班人员提醒,为了处理这种无效告警,可以在zabbix中设置维护 zabbix中的维护---维护期间:用来设 ...
- git 入门(转)
1. good https://github.com/521xueweihan/git-tips Git的奇技淫巧 2.如果之前未使用过 Git,可以学习 Git 小白教程入门 3.思维导图
- Kafka 详解(一)------简介
在前面几篇博客我们介绍过一种消息中间件——RabbitMQ,本篇博客我们介绍另外一个消息中间件——Kafka,Kafka是由LinkedIn开发的,使用Scala编写,是一种分布式,基于发布/订阅的消 ...
- 3.HttpSession
1 HttpSession概述 1.1 什么是HttpSesssion javax.servlet.http.HttpSession接口表示一个会话,我们可以把一个会话内需要共享的数据保存到HttSe ...
- 在Bootstrap开发框架的前端视图中使用@RenderPage实现页面内容模块化的隔离,减少复杂度
在很多开发的场景中,很多情况下我们需要考虑抽象.以及模块化等方面的内容,其目的就是为了使得开发的时候关注的变化内容更加少一些,整体开发更加简单化,从而减少开发的复杂度,在Winform开发的时候,往往 ...
- building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
Error msg: building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required. Ge ...
- vue脚手架搭建移动端项目--flexible.js
通过命令行 node -v 查看是否安装node环境 在 nodejs 和 webpack已安装的前提下,随便一个文件夹下,输入命令行 npm install vue-cli -g 安装完成后,通过 ...