首頁 > Java > java教程 > 主體

SpringBoot+Elasticsearch如何實現資料搜尋

WBOY
發布: 2023-05-12 21:04:04
轉載
1217 人瀏覽過

    一、簡介

    SpringBoot 連結ElasticSearch,主流的方式有以下四種方式

    • 方式一:透過Elastic Transport Client客戶端連接es 伺服器,底層基於TCP 協定透過transport 模組和遠端ES 服務端通信,不過,從V7.0 開始官方不建議使用,V8.0開始正式移除。

    • 方式二:透過Elastic Java Low Level Rest Client客戶端連接es 伺服器,底層基於HTTP 協定透過restful API 和遠端ES 服務端通信,只提供了最簡單、最基本的API,類似上篇文章給大家介紹的API 操作邏輯。

    • 方式三:透過Elastic Java High Level Rest Client客戶端連接es 伺服器,底層基於Elastic Java Low Level Rest Client客戶端做了一層封裝,提供了更進階得API且和Elastic Transport Client介面及參數保持一致,官方推薦的es 用戶端。

    • 方式四:透過JestClient客戶端連接es 伺服器,這是開源社群基於HTTP 協定開發的一款es 客戶端,官方宣稱介面及程式碼設計比ES 官方提供的Rest客戶端更簡潔、更合理,更好用,具有一定的ES 服務端版本相容性,但是更新速度不是很快,目前ES 版本已經出到V7.9,但是JestClient只支援V1.0~V6.X版本的ES。

    還有一個需要大家注意的地方,那就是版本號碼的相容!

    在開發過程中,大家尤其需要關註一下客戶端和服務端的版本號,要盡可能保持一致,例如服務端es 的版本號是6.8.2,那麼連接es 的客戶端版本號,最好也是6.8.2,即使因專案的原因不能保持一致,客戶端的版本號必須在6.0.0 ~6.8.2,不要超過伺服器的版本號,這樣客戶端才能保持正常工作,否則會出現很多意想不到的問題,假如客戶端是7.0.4的版本號,此時的程式會各種報錯,甚至沒辦法用!

    為什麼要這樣做呢?主要原因就是es 的服務端,高版本不相容低版本;es6 和es7 的某些API 請求參數結構有著很大的區別,所以客戶端和服務端版本號盡量保持一致。

    廢話也不多說了,直接上代碼!

    二、代碼實踐

    #本文採用的SpringBoot版本號是2.1.0.RELEASE,服務端es 的版本號碼是6.8.2,客戶端採用的是官方推薦的Elastic Java High Level Rest Client版本號碼是6.4.2,方便與SpringBoot的版本相容。

    2.1、匯入依賴

    <!--elasticsearch-->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>6.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>6.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.4.2</version>
    </dependency>
    登入後複製

    2.2、設定環境變數

    在application.properties全域設定檔中,設定elasticsearch自訂環境變數。

    elasticsearch.scheme=http
    elasticsearch.address=127.0.0.1:9200
    elasticsearch.userName=
    elasticsearch.userPwd=
    elasticsearch.socketTimeout=5000
    elasticsearch.connectTimeout=5000
    elasticsearch.connectionRequestTimeout=5000
    登入後複製

    2.3、建立 elasticsearch 的 config 類別

    @Configuration
    public class ElasticsearchConfiguration {
    
        private static final Logger log = LoggerFactory.getLogger(ElasticsearchConfiguration.class);
    
    
        private static final int ADDRESS_LENGTH = 2;
    
        @Value("${elasticsearch.scheme:http}")
        private String scheme;
    
        @Value("${elasticsearch.address}")
        private String address;
    
        @Value("${elasticsearch.userName}")
        private String userName;
    
        @Value("${elasticsearch.userPwd}")
        private String userPwd;
    
        @Value("${elasticsearch.socketTimeout:5000}")
        private Integer socketTimeout;
    
        @Value("${elasticsearch.connectTimeout:5000}")
        private Integer connectTimeout;
    
        @Value("${elasticsearch.connectionRequestTimeout:5000}")
        private Integer connectionRequestTimeout;
    
        /**
         * 初始化客户端
         * @return
         */
        @Bean(name = "restHighLevelClient")
        public RestHighLevelClient restClientBuilder() {
            HttpHost[] hosts = Arrays.stream(address.split(","))
                    .map(this::buildHttpHost)
                    .filter(Objects::nonNull)
                    .toArray(HttpHost[]::new);
            RestClientBuilder restClientBuilder = RestClient.builder(hosts);
            // 异步参数配置
            restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
                httpClientBuilder.setDefaultCredentialsProvider(buildCredentialsProvider());
                return httpClientBuilder;
            });
    
            // 异步连接延时配置
            restClientBuilder.setRequestConfigCallback(requestConfigBuilder -> {
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
                requestConfigBuilder.setSocketTimeout(socketTimeout);
                requestConfigBuilder.setConnectTimeout(connectTimeout);
                return requestConfigBuilder;
            });
    
            return new RestHighLevelClient(restClientBuilder);
        }
    
    
        /**
         * 根据配置创建HttpHost
         * @param s
         * @return
         */
        private HttpHost buildHttpHost(String s) {
            String[] address = s.split(":");
            if (address.length == ADDRESS_LENGTH) {
                String ip = address[0];
                int port = Integer.parseInt(address[1]);
                return new HttpHost(ip, port, scheme);
            } else {
                return null;
            }
        }
    
        /**
         * 构建认证服务
         * @return
         */
        private CredentialsProvider buildCredentialsProvider(){
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName,
                    userPwd));
            return credentialsProvider;
        }
    }
    登入後複製

    至此,客戶端設定完畢,專案啟動的時候,會自動注入Spring的ioc容器裡面。

    2.4、索引管理

    es 中最重要的就是索引庫,客戶端如何建立呢?請看下文!

    建立索引

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class IndexJunit {
    
    
        @Autowired
        private RestHighLevelClient client;
    
        /**
         * 创建索引(简单模式)
         * @throws IOException
         */
        @Test
        public void createIndex() throws IOException {
            CreateIndexRequest request = new CreateIndexRequest("cs_index");
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            System.out.println(response.isAcknowledged());
        }
    
    
        /**
         * 创建索引(复杂模式)
         * 可以直接把对应的文档结构也一并初始化
         * @throws IOException
         */
        @Test
        public void createIndexComplete() throws IOException {
            CreateIndexRequest request = new CreateIndexRequest();
            //索引名称
            request.index("cs_index");
            //索引配置
            Settings settings = Settings.builder()
                    .put("index.number_of_shards", 3)
                    .put("index.number_of_replicas", 1)
                    .build();
            request.settings(settings);
    
            //映射结构字段
            Map<String, Object> properties = new HashMap();
            properties.put("id", ImmutableBiMap.of("type", "text"));
            properties.put("name", ImmutableBiMap.of("type", "text"));
            properties.put("sex", ImmutableBiMap.of("type", "text"));
            properties.put("age", ImmutableBiMap.of("type", "long"));
            properties.put("city", ImmutableBiMap.of("type", "text"));
            properties.put("createTime", ImmutableBiMap.of("type", "long"));
            Map<String, Object> mapping = new HashMap<>();
            mapping.put("properties", properties);
            //添加一个默认类型
            System.out.println(JSON.toJSONString(request));
            request.mapping("_doc",mapping);
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            System.out.println(response.isAcknowledged());
        }
    
    }
    登入後複製

    刪除索引

    #
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class IndexJunit {
    
    
        @Autowired
        private RestHighLevelClient client;
    
        /**
         * 删除索引
         * @throws IOException
         */
        @Test
        public void deleteIndex() throws IOException {
            DeleteIndexRequest request = new DeleteIndexRequest("cs_index1");
            AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
            System.out.println(response.isAcknowledged());
        }
    
    
    }
    登入後複製

    查詢索引

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class IndexJunit {
    
    
        @Autowired
        private RestHighLevelClient client;
    
        /**
         * 查询索引
         * @throws IOException
         */
        @Test
        public void getIndex() throws IOException {
            // 创建请求
            GetIndexRequest request = new GetIndexRequest();
            request.indices("cs_index");
            // 执行请求,获取响应
            GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        }
    
    }
    登入後複製

    查詢索引是否存在

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class IndexJunit {
    
    
        @Autowired
        private RestHighLevelClient client;
    
        /**
         * 检查索引是否存在
         * @throws IOException
         */
        @Test
        public void exists() throws IOException {
            // 创建请求
            GetIndexRequest request = new GetIndexRequest();
            request.indices("cs_index");
            // 执行请求,获取响应
            boolean response = client.indices().exists(request, RequestOptions.DEFAULT);
            System.out.println(response);
        }
    
    }
    登入後複製

    查詢所有的索引名稱

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class IndexJunit {
    
    
        @Autowired
        private RestHighLevelClient client;
    
        /**
         * 查询所有的索引名称
         * @throws IOException
         */
        @Test
        public void getAllIndices() throws IOException {
            GetAliasesRequest request = new GetAliasesRequest();
            GetAliasesResponse response =  client.indices().getAlias(request,RequestOptions.DEFAULT);
            Map<String, Set<AliasMetaData>> map = response.getAliases();
            Set<String> indices = map.keySet();
            for (String key : indices) {
                System.out.println(key);
            }
        }
    
    }
    登入後複製

    查詢索引對應欄位##

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class IndexJunit {
    
    
        @Autowired
        private RestHighLevelClient client;
    
        /**
         * 查询索引映射字段
         * @throws IOException
         */
        @Test
        public void getMapping() throws IOException {
            GetMappingsRequest request = new GetMappingsRequest();
            request.indices("cs_index");
            request.types("_doc");
            GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        }
    
    
    }
    登入後複製

    新增索引映射欄位

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class IndexJunit {
    
    
        @Autowired
        private RestHighLevelClient client;
    
        /**
         * 添加索引映射字段
         * @throws IOException
         */
        @Test
        public void addMapping() throws IOException {
            PutMappingRequest request = new PutMappingRequest();
            request.indices("cs_index");
            request.type("_doc");
    
            //添加字段
            Map<String, Object> properties = new HashMap();
            properties.put("accountName", ImmutableBiMap.of("type", "keyword"));
            Map<String, Object> mapping = new HashMap<>();
            mapping.put("properties", properties);
            request.source(mapping);
            PutMappingResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT);
            System.out.println(response.isAcknowledged());
        }
    
    
    }
    登入後複製

    2.5、文件管理

    所謂文檔,就是在索引裡面新增數據,方便進行資料查詢,詳細操作內容,請看下文!

    新增文件

    ublic class UserDocument {
    
        private String id;
        private String name;
        private String sex;
        private Integer age;
        private String city;
        private Date createTime;
    
        //省略get、set...
    }
    登入後複製
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class DocJunit {
    
        @Autowired
        private RestHighLevelClient client;
    
    
        /**
         * 添加文档
         * @throws IOException
         */
        @Test
        public void addDocument() throws IOException {
            // 创建对象
            UserDocument user = new UserDocument();
            user.setId("1");
            user.setName("里斯");
            user.setCity("武汉");
            user.setSex("男");
            user.setAge(20);
            user.setCreateTime(new Date());
    
            // 创建索引,即获取索引
            IndexRequest request = new IndexRequest();
            // 外层参数
            request.id("1");
            request.index("cs_index");
            request.type("_doc");
            request.timeout(TimeValue.timeValueSeconds(1));
            // 存入对象
            request.source(JSON.toJSONString(user), XContentType.JSON);
            // 发送请求
            System.out.println(request.toString());
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        }
    
    }
    登入後複製

    更新文件

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class DocJunit {
    
        @Autowired
        private RestHighLevelClient client;
    
    
        /**
         * 更新文档(按需修改)
         * @throws IOException
         */
        @Test
        public void updateDocument() throws IOException {
            // 创建对象
            UserDocument user = new UserDocument();
            user.setId("2");
            user.setName("程咬金");
            user.setCreateTime(new Date());
            // 创建索引,即获取索引
            UpdateRequest request = new UpdateRequest();
            // 外层参数
            request.id("2");
            request.index("cs_index");
            request.type("_doc");
            request.timeout(TimeValue.timeValueSeconds(1));
            // 存入对象
            request.doc(JSON.toJSONString(user), XContentType.JSON);
            // 发送请求
            System.out.println(request.toString());
            UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        }
    
    
    }
    登入後複製

    刪除文件

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class DocJunit {
    
        @Autowired
        private RestHighLevelClient client;
    
    
        /**
         * 删除文档
         * @throws IOException
         */
        @Test
        public void deleteDocument() throws IOException {
            // 创建索引,即获取索引
            DeleteRequest request = new DeleteRequest();
            // 外层参数
            request.id("1");
            request.index("cs_index");
            request.type("_doc");
            request.timeout(TimeValue.timeValueSeconds(1));
            // 发送请求
            System.out.println(request.toString());
            DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        }
    
    
    }
    登入後複製

    查詢文檔是不是存在

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class DocJunit {
    
        @Autowired
        private RestHighLevelClient client;
    
    
        /**
         * 查询文档是不是存在
         * @throws IOException
         */
        @Test
        public void exists() throws IOException {
            // 创建索引,即获取索引
            GetRequest request = new GetRequest();
            // 外层参数
            request.id("3");
            request.index("cs_index");
            request.type("_doc");
            // 发送请求
            System.out.println(request.toString());
            boolean response = client.exists(request, RequestOptions.DEFAULT);
            System.out.println(response);
        }
    }
    登入後複製

    透過ID 查詢指定文件

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class DocJunit {
    
        @Autowired
        private RestHighLevelClient client;
    
    
        /**
         * 通过ID,查询指定文档
         * @throws IOException
         */
        @Test
        public void getById() throws IOException {
            // 创建索引,即获取索引
            GetRequest request = new GetRequest();
            // 外层参数
            request.id("1");
            request.index("cs_index");
            request.type("_doc");
            // 发送请求
            System.out.println(request.toString());
            GetResponse response = client.get(request, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        }
    }
    登入後複製

    批次新增文件

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ElasticSearchApplication.class)
    public class DocJunit {
    
        @Autowired
        private RestHighLevelClient client;
    
    
        /**
         * 批量添加文档
         * @throws IOException
         */
        @Test
        public void batchAddDocument() throws IOException {
            // 批量请求
            BulkRequest bulkRequest = new BulkRequest();
            bulkRequest.timeout(TimeValue.timeValueSeconds(10));
            // 创建对象
            List<UserDocument> userArrayList = new ArrayList<>();
            userArrayList.add(new UserDocument("张三", "男", 30, "武汉"));
            userArrayList.add(new UserDocument("里斯", "女", 31, "北京"));
            userArrayList.add(new UserDocument("王五", "男", 32, "武汉"));
            userArrayList.add(new UserDocument("赵六", "女", 33, "长沙"));
            userArrayList.add(new UserDocument("七七", "男", 34, "武汉"));
            // 添加请求
            for (int i = 0; i < userArrayList.size(); i++) {
                userArrayList.get(i).setId(String.valueOf(i));
                IndexRequest indexRequest = new IndexRequest();
                // 外层参数
                indexRequest.id(String.valueOf(i));
                indexRequest.index("cs_index");
                indexRequest.type("_doc");
                indexRequest.timeout(TimeValue.timeValueSeconds(1));
                indexRequest.source(JSON.toJSONString(userArrayList.get(i)), XContentType.JSON);
                bulkRequest.add(indexRequest);
            }
            // 执行请求
            BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println(response.status());
        }
    
    }
    登入後複製

    以上是SpringBoot+Elasticsearch如何實現資料搜尋的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    相關標籤:
    來源:yisu.com
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板