最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot+Elasticsearch實現(xiàn)數(shù)據(jù)搜索的方法詳解

 更新時間:2022年05月30日 15:24:34   作者:鴨血粉絲Tang  
Elasticsearch是一個基于Lucene的搜索服務(wù)器。它提供了一個分布式多用戶能力的全文搜索引擎,基于RESTful?web接口。本文將利用SpringBoot整合Elasticsearch實現(xiàn)海量級數(shù)據(jù)搜索,需要的可以參考一下

一、簡介

在上篇 ElasticSearch 文章中,我們詳細的介紹了 ElasticSearch 的各種 api 使用。

實際的項目開發(fā)過程中,我們通?;谀承┲髁骺蚣芷脚_進行技術(shù)開發(fā),比如 SpringBoot,今天我們就以 SpringBoot 整合

ElasticSearch 為例,給大家詳細的介紹 ElasticSearch 的使用!

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

  • 方式一:通過Elastic Transport Client客戶端連接 es 服務(wù)器,底層基于 TCP 協(xié)議通過 transport 模塊和遠程 ES 服務(wù)端通信,不過,從 V7.0 開始官方不建議使用,V8.0開始正式移除。
  • 方式二:通過Elastic Java Low Level Rest Client客戶端連接 es 服務(wù)器,底層基于 HTTP 協(xié)議通過 restful API 來和遠程 ES 服務(wù)端通信,只提供了最簡單最基本的 API,類似于上篇文章中給大家介紹的 API 操作邏輯。
  • 方式三:通過Elastic Java High Level Rest Client客戶端連接 es 服務(wù)器,底層基于Elastic Java Low Level Rest Client客戶端做了一層封裝,提供了更高級得 API 且和Elastic Transport Client接口及參數(shù)保持一致,官方推薦的 es 客戶端。
  • 方式四:通過JestClient客戶端連接 es 服務(wù)器,這是開源社區(qū)基于 HTTP 協(xié)議開發(fā)的一款 es 客戶端,官方宣稱接口及代碼設(shè)計比 ES 官方提供的 Rest 客戶端更簡潔、更合理,更好用,具有一定的 ES 服務(wù)端版本兼容性,但是更新速度不是很快,目前 ES 版本已經(jīng)出到 V7.9,但是JestClient只支持 V1.0~V6.X 版 本的 ES。

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

在開發(fā)過程中,大家尤其需要關(guān)注一下客戶端和服務(wù)端的版本號,要盡可能保持一致,比如服務(wù)端 es 的版本號是6.8.2,那么連接 es 的客戶端版本號,最好也是6.8.2,即使因項目的原因不能保持一致,客戶端的版本號必須在6.0.0 ~6.8.2,不要超過服務(wù)器的版本號,這樣客戶端才能保持正常工作,否則會出現(xiàn)很多意想不到的問題,假如客戶端是7.0.4的版本號,此時的程序會各種報錯,甚至沒辦法用!

為什么要這樣做呢?主要原因就是 es 的服務(wù)端,高版本不兼容低版本;es6 和 es7 的某些 API 請求參數(shù)結(jié)構(gòu)有著很大的區(qū)別,所以客戶端和服務(wù)端版本號盡量保持一致。

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

二、代碼實踐

本文采用的SpringBoot版本號是2.1.0.RELEASE,服務(wù)端 es 的版本號是6.8.2,客戶端采用的是官方推薦的Elastic Java High Level Rest Client版本號是6.4.2,方便與SpringBoot的版本兼容。

2.1、導(dǎo)入依賴

<!--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、配置環(huán)境變量

在application.properties全局配置文件中,配置elasticsearch自定義環(huán)境變量。

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、創(chuàng)建 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);
        // 異步參數(shù)配置
        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);
    }


    /**
     * 根據(jù)配置創(chuàng)建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;
        }
    }

    /**
     * 構(gòu)建認證服務(wù)
     * @return
     */
    private CredentialsProvider buildCredentialsProvider(){
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName,
                userPwd));
        return credentialsProvider;
    }
}

至此,客戶端配置完畢,項目啟動的時候,會自動注入到Spring的ioc容器里面。

2.4、索引管理

es 中最重要的就是索引庫,客戶端如何創(chuàng)建呢?請看下文!

創(chuàng)建索引

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {


    @Autowired
    private RestHighLevelClient client;

    /**
     * 創(chuàng)建索引(簡單模式)
     * @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());
    }


    /**
     * 創(chuàng)建索引(復(fù)雜模式)
     * 可以直接把對應(yīng)的文檔結(jié)構(gòu)也一并初始化
     * @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);

        //映射結(jié)構(gòu)字段
        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 {
        // 創(chuàng)建請求
        GetIndexRequest request = new GetIndexRequest();
        request.indices("cs_index");
        // 執(zhí)行請求,獲取響應(yīng)
        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 {
        // 創(chuàng)建請求
        GetIndexRequest request = new GetIndexRequest();
        request.indices("cs_index");
        // 執(zhí)行請求,獲取響應(yīng)
        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、文檔管理

所謂文檔,就是向索引里面添加數(shù)據(jù),方便進行數(shù)據(jù)查詢,詳細操作內(nèi)容,請看下文!

添加文檔

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 {
        // 創(chuàng)建對象
        UserDocument user = new UserDocument();
        user.setId("1");
        user.setName("里斯");
        user.setCity("武漢");
        user.setSex("男");
        user.setAge(20);
        user.setCreateTime(new Date());

        // 創(chuàng)建索引,即獲取索引
        IndexRequest request = new IndexRequest();
        // 外層參數(shù)
        request.id("1");
        request.index("cs_index");
        request.type("_doc");
        request.timeout(TimeValue.timeValueSeconds(1));
        // 存入對象
        request.source(JSON.toJSONString(user), XContentType.JSON);
        // 發(fā)送請求
        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 {
        // 創(chuàng)建對象
        UserDocument user = new UserDocument();
        user.setId("2");
        user.setName("程咬金");
        user.setCreateTime(new Date());
        // 創(chuàng)建索引,即獲取索引
        UpdateRequest request = new UpdateRequest();
        // 外層參數(shù)
        request.id("2");
        request.index("cs_index");
        request.type("_doc");
        request.timeout(TimeValue.timeValueSeconds(1));
        // 存入對象
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        // 發(fā)送請求
        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 {
        // 創(chuàng)建索引,即獲取索引
        DeleteRequest request = new DeleteRequest();
        // 外層參數(shù)
        request.id("1");
        request.index("cs_index");
        request.type("_doc");
        request.timeout(TimeValue.timeValueSeconds(1));
        // 發(fā)送請求
        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 {
        // 創(chuàng)建索引,即獲取索引
        GetRequest request = new GetRequest();
        // 外層參數(shù)
        request.id("3");
        request.index("cs_index");
        request.type("_doc");
        // 發(fā)送請求
        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 {
        // 創(chuàng)建索引,即獲取索引
        GetRequest request = new GetRequest();
        // 外層參數(shù)
        request.id("1");
        request.index("cs_index");
        request.type("_doc");
        // 發(fā)送請求
        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));
        // 創(chuàng)建對象
        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();
            // 外層參數(shù)
            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);
        }
        // 執(zhí)行請求
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

}

三、小結(jié)

本文主要圍繞 SpringBoot 整合 ElasticSearch 接受數(shù)據(jù)的插入和搜索使用技巧,在實際的使用過程中,版本號尤其的重要,不同版本的 es,對應(yīng)的 api 是不一樣的。

以上就是SpringBoot+Elasticsearch實現(xiàn)數(shù)據(jù)搜索的方法詳解的詳細內(nèi)容,更多關(guān)于SpringBoot Elasticsearch數(shù)據(jù)搜索的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java中HashMap和HashTable區(qū)別

    Java中HashMap和HashTable區(qū)別

    HashMap和Hashtable都是Java常見的基于哈希表實現(xiàn)的Map接口的實現(xiàn)類,本文主要介紹了Java中HashMap和HashTable區(qū)別,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • 解決SpringCloud Config結(jié)合github無法讀取配置的問題

    解決SpringCloud Config結(jié)合github無法讀取配置的問題

    這篇文章主要介紹了解決SpringCloud Config結(jié)合github無法讀取配置的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Dubbo+Nacos服務(wù)啟動報錯,返回unknown user的問題

    Dubbo+Nacos服務(wù)啟動報錯,返回unknown user的問題

    這篇文章主要介紹了Dubbo+Nacos服務(wù)啟動報錯,返回unknown user的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 控制臺顯示java冒泡排序流程示例

    控制臺顯示java冒泡排序流程示例

    這篇文章主要介紹了控制臺顯示java冒泡排序流程示例,需要的朋友可以參考下
    2014-03-03
  • 簡單工廠模式_動力節(jié)點Java學(xué)院整理

    簡單工廠模式_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了簡單工廠模式的相關(guān)資料,和大家一起學(xué)習(xí)靜態(tài)工廠方法模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • java判斷Long類型的方法和實例代碼

    java判斷Long類型的方法和實例代碼

    在本篇文章里小編給大家整理的是關(guān)于java判斷Long類型的方法和實例代碼,對此有需要的朋友們跟著學(xué)習(xí)參考下。
    2020-02-02
  • springboot文件虛擬路徑映射方式

    springboot文件虛擬路徑映射方式

    這篇文章主要介紹了springboot文件虛擬路徑映射方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java的split方法使用詳解

    Java的split方法使用詳解

    這篇文章主要詳細介紹了Java的split方法使用說明,十分的細致全面,有需要的小伙伴可以參考下。
    2015-07-07
  • Spring AOP實現(xiàn)權(quán)限檢查的功能

    Spring AOP實現(xiàn)權(quán)限檢查的功能

    這篇文章主要介紹了Spring AOP實現(xiàn)權(quán)限檢查的功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Jmeter環(huán)境搭建及安裝步驟

    Jmeter環(huán)境搭建及安裝步驟

    Jmeter是純Java開發(fā)的,能夠運行Java程序的系統(tǒng)一般都可以運行Jmeter,本文以windows下安裝步驟為例分步驟給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2021-12-12

最新評論

花莲县| 健康| 金平| 吉隆县| 水城县| 白山市| 永寿县| 东阿县| 四子王旗| 伊宁县| 孝昌县| 桐梓县| 霞浦县| 肇庆市| 聂拉木县| 曲阜市| 图片| 铜鼓县| 垦利县| 德兴市| 太康县| 深圳市| 綦江县| 东兰县| 民乐县| 通城县| 农安县| 铜山县| 大名县| 浦北县| 天长市| 龙州县| 盐山县| 平乐县| 绥中县| 仁寿县| 颍上县| 宜宾县| 富蕴县| 岳西县| 无棣县|