elasticSearch-JavaRest 高级客户端入门

6 JavaRest 高级客户端入门

1 JavaRest 高级客户端简介

elasticsearch 存在三种Java客户端。

  1. Transport Client
  2. Java Low Level Rest Client(低级rest客户端)
  3. Java High Level REST Client(高级rest客户端)
    这三者的区别是:
    TransportClient没有使用RESTful风格的接口,而是二进制的方式传输数据。
    ES官方推出了Java Low Level REST Client,它支持RESTful。但是缺点是因为
    TransportClient的使用者把代码迁移到Low Level REST Client的工作量比较大。
    ES官方推出Java High Level REST Client,它是基于Java Low Level REST Client的封装,并且API接收参数和返回值和TransportClient是一样的,使得代码迁移变得容易并且支持了RESTful的风格,兼容了这两种客户端的优点。强烈建议ES5及其以后的版本使用Java High Level REST Client。

2 准备工作:新建工程,引入依赖


    
        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        6.5.3
    

3 快速入门

1 新增和修改数据

插入单条数据:
HttpHost : url地址封装
RestClientBuilder: rest客户端构建器
RestHighLevelClient: rest高级客户端
IndexRequest: 新增或修改请求
IndexResponse:新增或修改的响应结

package cn.laixueit.demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ElasticSearchDemo {
    public static void main(String[] args) throws IOException {
        // 1 建立连接
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder clientBuilder = RestClient.builder(http);
        RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
        // 2 发送数据

        IndexRequest indexRequest = new IndexRequest("sku","doc","3");
        Map skuMap = new HashMap();
        skuMap.put("name","华为p30pro");
        skuMap.put("brandName","华为");
        skuMap.put("categoryName","手机");
        skuMap.put("price",1010221);
        skuMap.put("createTime","2019-05-01");
        skuMap.put("saleNum",101021);
        skuMap.put("commentNum",10102321);
        Map spec=new HashMap();
        spec.put("网络制式","移动4G");
        spec.put("屏幕尺寸","5");
        skuMap.put("spec",spec);
        indexRequest.source(skuMap);
        // 3 返回响应
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        int status = response.status().getStatus();
        System.out.println(status);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UBcZX0VM-1652341978453)(_v_images/20200618183844673_13211.png)]
如果ID不存在则新增,如果ID存在则修改。
批处理请求:
BulkRequest: 批量请求(用于增删改操作)
BulkResponse:批量请求(用于增删改操作)
修改测试:源代码修改字段属性继续提交
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mc3woJSY-1652341978454)(_v_images/20200618184053908_1248.png)]

2 批量请求(用于增删改操作)

package cn.laixueit.demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ElasticSearchDemo {
    public static void main(String[] args) throws IOException {
        // 1 建立连接
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder clientBuilder = RestClient.builder(http);
        RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
        // 2 发送数据
        BulkRequest bulkRequest = new BulkRequest();

        IndexRequest indexRequest = new IndexRequest("sku","doc","5");

        Map skuMap = new HashMap();
        skuMap.put("name","华为p5");
        skuMap.put("brandName","华为");
        skuMap.put("categoryName","手机");
        skuMap.put("price",1010221);
        skuMap.put("createTime",new Date());
        skuMap.put("saleNum",101021);
        skuMap.put("commentNum",10102321);
        Map spec=new HashMap();
        spec.put("网络制式","移动4G");
        spec.put("屏幕尺寸","5");
        skuMap.put("spec",spec);
        indexRequest.source(skuMap);

        IndexRequest indexRequest2 = new IndexRequest("sku","doc","6");
        Map skuMap2 = new HashMap();
        skuMap2.put("name","华为p6 ");
        skuMap2.put("brandName","华为");
        skuMap2.put("categoryName","手机");
        skuMap2.put("price",1010221);
        skuMap2.put("createTime",new Date());
        skuMap2.put("saleNum",101021);
        skuMap2.put("commentNum",10102321);
        Map spec2=new HashMap();
        spec2.put("网络制式","移动4G");
        spec2.put("屏幕尺寸","5");
        skuMap2.put("spec",spec2);
        indexRequest2.source(skuMap2);

        bulkRequest.add(indexRequest);
        bulkRequest.add(indexRequest2);
        // 3 返回响应
//        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        int status = bulkResponse.status().getStatus();
        System.out.println(status);

        client.close();
    }
}

3 匹配查询

SearchRequest: 查询请求对象
SearchResponse:查询响应对象
SearchSourceBuilder:查询源构建器
MatchQueryBuilder:匹配查询构建器

package cn.laixueit.demo;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Test2 {
    public static void main(String[] args) throws IOException {
        // 1 建立连接
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder clientBuilder = RestClient.builder(http);
        RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
        // 2 发送数据
        SearchRequest searchRequest = new SearchRequest("sku");
        searchRequest.types("doc");

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "小米");
        SearchSourceBuilder query = sourceBuilder.query(queryBuilder);

        searchRequest.source(sourceBuilder);


        // 3 返回响应
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        Arrays.stream(hits).forEach((r)->{
            System.out.println(r.getSourceAsString());
        });

        client.close();
    }
}

4 布尔与词条查询

BoolQueryBuilder:布尔查询构建器
TermQueryBuilder:词条查询构建器
QueryBuilders:查询构建器工厂

package cn.laixueit.demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Arrays;

/**
 * BoolQueryBuilder:布尔查询构建器
 * TermQueryBuilder:词条查询构建器
 * QueryBuilders:查询构建器工厂
 */
public class Test3 {
    public static void main(String[] args) throws IOException {
        // 1 建立连接
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder clientBuilder = RestClient.builder(http);
        RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
        // 2 发送数据

        SearchRequest searchRequest = new SearchRequest("sku");
        searchRequest.types("doc");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "华为");
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");

        boolQuery.must(matchQueryBuilder);
        boolQuery.must(termQueryBuilder);

        searchSourceBuilder.query(boolQuery);

        searchRequest.source(searchSourceBuilder);


        // 3 返回响应
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        Arrays.stream(hits).forEach((r)->{
            System.out.println(r.getSourceAsString());
        });

        client.close();
    }
}

5 过滤查询

package cn.laixueit.demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Arrays;

public class Test4 {
    public static void main(String[] args) throws IOException {
        // 1 建立连接
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder clientBuilder = RestClient.builder(http);
        RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
        // 2 发送数据

        SearchRequest searchRequest = new SearchRequest("sku");
        searchRequest.types("doc");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");

        boolQuery.filter(termQueryBuilder);

        searchSourceBuilder.query(boolQuery);

        searchRequest.source(searchSourceBuilder);


        // 3 返回响应
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        Arrays.stream(hits).forEach((r)->{
            System.out.println(r.getSourceAsString());
        });

        client.close();
    }
}

你可能感兴趣的:(elasticsearch,搜索引擎,java)