Java實現調用ElasticSearch API的示例詳解

java操作es有兩種方式

1.通過操作es的9300端口,9300是tcp端口,集群節點之間通信也是通過9300端口,會通過9300和es建立一個長連接,下面的es的依賴可以直接操作

但是隨著es的版本的提升spring-data需要封裝不同版本的es的jar包,好像還沒封裝到這個版本(2019),另外官方也不推薦通過9300來操作es,而且這種方式在es8以後將被廢棄

2.通過9200操作,發送http請求

  • JestClient,非官方,更新慢
  • RestTemplate(springboot),模擬發http請求,es很多操作需要自己封裝,麻煩
  • HttpCLient,同上
  • Elasticsearch-Rest-Client,官方RestClient,封裝瞭ES操作,API層次分明,上手簡單

我們在瀏覽官方文檔的時候發現,js可以直接操作es,那為什麼我們不直接用js來操作es呢?

  • 出於安全,因為es集群屬於後端集群服務器,端口一般不對外暴露,如果對外暴露,會被別人惡意利用
  • js對es支持度有些低,我們如果用js操作的話,不需要通過官網提供的api,我們直接發送ajax請求,用原生es語句即可

其中,官網的java api是通過9300來操作的,java rest api是通過9200來操作的

官網中有Java Low Level REST Client和Java High Level REST Client,關系就和mybatis和jdbc一樣

Elasticsearch-Rest-Client(官方,推薦)

這個不是專門看視頻學習的,是谷粒商城的時候,跟著老師敲的,所以其實就是一個對谷粒商城涉及到這塊兒的一個總結,版本什麼的自然也就是用的它的。

這算是我總結的一個api,沒有真實對照的使用過,隻是為瞭理清思路。

maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.atlinxi.gulimall</groupId>
    <artifactId>gulimall-search</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gulimall-search</name>
    <description>elasticsearch檢索服務</description>
    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.4.2</elasticsearch.version>
        <spring-cloud.version>2020.0.4</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.atlinxi.gulimall</groupId>
            <artifactId>gulimall-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-search

es配置類

package com.atlinxi.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * es配置類,給容器中註入一個RestHighLevelClient
 */
@Configuration
public class GulimallElasticSearchConfig {

    // 後端訪問es的時候,出於安全考慮,可以攜帶一個請求頭
    // 現在暫時不用
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }


    @Bean
    public RestHighLevelClient esRestClient(){

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.56.10", 9200, "http")
//                        new HttpHost("localhost", 9201, "http")
                ));
        return client;

    }


}

導包

package com.atlinxi.gulimall.search;

import com.alibaba.fastjson.JSON;
import com.atlinxi.gulimall.search.config.GulimallElasticSearchConfig;
import lombok.Data;
import lombok.ToString;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.Avg;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@Autowired
RestHighLevelClient restHighLevelClient;

查詢

// 1. 創建檢索請求
SearchRequest searchRequest = new SearchRequest();

// 2. 指定索引
searchRequest.indices("bank");


// 3. 指定DSL,檢索條件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

// 3.1 構造檢索條件
        // 所有的函數名都對應原生es DSL語句
//        searchSourceBuilder.query();
//        searchSourceBuilder.from();
//        searchSourceBuilder.size();
//        searchSourceBuilder.aggregation();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.matchQuery("skuTitle", param.getKeyword()));
boolQuery.filter(QueryBuilders.termQuery("catalogId", param.getCatalog3Id()));
boolQuery.filter(QueryBuilders.termsQuery("brandId", param.getBrandId()));

BoolQueryBuilder nestedBoolQuery = QueryBuilders.boolQuery();
QueryBuilders.nestedQuery("attrs", nestedBoolQuery, ScoreMode.None);
boolQuery.filter(nestedQuery);

QueryBuilders.rangeQuery("skuPrice");
boolQuery.filter(rangeQuery);

searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
searchSourceBuilder.query(boolQuery);
searchSourceBuilder.sort(field, order);
sourceBuilder.from(0);
sourceBuilder.size(10);

HighlightBuilder builder = new HighlightBuilder();
builder.field("skuTitle");
builder.preTags("<b style='color:red'>");
builder.postTags("</b>");
sourceBuilder.highlighter(builder);

// 3.2 聚合
// 按照年齡的值分佈進行聚合
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
// 計算平均薪資
AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
AggregationBuilders.nested("attr_agg", "attrs");
        
searchSourceBuilder.aggregation(balanceAvg);
searchSourceBuilder.aggregation(ageAgg);



// 4. 執行檢索請求
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);


// 5.獲取響應結果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
searchHit.getSourceAsString();


// 3.1 獲取聚合結果
Aggregations aggregations = searchResponse.getAggregations();
Terms ageAgg1 = aggregations.get("ageAgg");
// 返回值為List<? extends Terms.Bucket>
ageAgg1.getBuckets()
bucket.getKeyAsString();

aggregations.get("balanceAvg");

保存更新

// 添加數據有多種方式,例如hashmap、直接將json粘在這兒
IndexRequest request = new IndexRequest("users");
request.id("1");
//        request.source("userName","zhangsan","age",12,"gender","男");

//        String jsonString = "{" +
//                "\"user\":\"kimchy\"," +
//                "\"postDate\":\"2013-01-30\"," +
//                "\"message\":\"trying out Elasticsearch\"" +
//                "}";
//        request.source(jsonString, XContentType.JSON);

User user = new User();
user.setUserName("zs");
user.setAge(12);
user.setGender("man");
String jsonString = JSON.toJSONString(user);
request.source(jsonString, XContentType.JSON);

// 執行保存/更新操作
IndexResponse index = restHighLevelClient.index(request, GulimallElasticSearchConfig.COMMON_OPTIONS);





// 批量保存
// 1. 建立索引 product 建立好映射關系(kibana操作)
// 2. 給es中保存這些數據
BulkRequest bulkRequest = new BulkRequest();
IndexRequest indexRequest = new IndexRequest(EsConstant.Product_INDEX);
indexRequest.id(skuEsModel.getSkuId().toString());
String s = JSON.toJSONString(skuEsModel);
indexRequest.source(s, XContentType.JSON);
bulkRequest.add(indexRequest);
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);

// todo 如果批量錯誤,處理錯誤
boolean b = bulk.hasFailures();
bulk.getItems()

Spring Data ElasticSearch

Spring Data可以極大的簡化JPA的寫法,可以在幾乎不用寫實現的情況下,實現對數據的訪問和操作。除瞭CRUD外,還包括如分頁、排序等一些常用的功能。

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:es="http://www.springframework.org/schema/data/elasticsearch"


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd


       http://www.springframework.org/schema/data/elasticsearch
       http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd

">
    <!--
        如果你希望 進一步瞭解 xsd相關的知識 請求百度去 百度不著瞭  http://www.jk1123.com/?p=124
    -->

    <!--
        配置 client 連上 es

       配置 dao層的掃描

       配置其他  一個叫做 esTemplate  就是一個簡單對應client封裝


    -->


     <es:transport-client id="client" cluster-nodes="127.0.0.1:9300" cluster-name="my-elasticsearch"/>

    <es:repositories base-package="com.itheima.dao"></es:repositories>

    <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client"></constructor-arg>
    </bean>

</beans>

實體類

實體類的無參構造必須有,否則查詢出來的對象無法映射到實體類

@Document(indexName=“blob3”,type=“article”):

  • indexName:索引的名稱(必填項)
  • type:索引的類型

@Id:主鍵的唯一標識

@Field(index=true,analyzer=“ik_smart”,store=true,searchAnalyzer=“ik_smart”,type = FieldType.text)

  • analyzer:存儲時使用的分詞器
  • searchAnalyze:搜索時使用的分詞器
  • store:是否存儲
  • type: 數據類型
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

//@Document 文檔對象 (索引信息、文檔類型 )
@Document(indexName="test03",type="book")
public class Book {

    //@Id 文檔主鍵 唯一標識
    @Id
    //@Field 每個文檔的字段配置(類型、是否分詞、是否存儲、分詞器 )
    @Field(store = true, index = false, type = FieldType.Integer)
    private Integer id;
    @Field(analyzer = "ik_max_word", store = true, type = FieldType.text)
    private String title;
    @Field(analyzer = "ik_max_word", store = true, type = FieldType.text)
    private String content;
    @Field(index = false, store = true, type = FieldType.Long)
    private Long sales;

dao

import com.itheima.domain.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BookDao extends ElasticsearchRepository<Book, Integer> {

// 除瞭系統自帶的方法,還可以自定義命名
    List<Book> findByContent(String content);

    List<Book> findByContentAndTitle(String content, String title);

    Page<Book> findByContent(String content, Pageable pageable);

}

crud

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class AppTest {

    @Resource
    private BookDao bookDao;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    // 創建索引庫
    @Test
    public void testCreatIndex(){
        elasticsearchTemplate.createIndex(Book.class);
        elasticsearchTemplate.putMapping(Book.class);
    }


// 新增或者更新
    @Test
    public void save(){

        Book book = new Book(20,"20","20",20L);

        bookDao.save(book);
    }

    // 刪除
    @Test
    public void testDelete(){
        bookDao.deleteById(20);
    }

    @Test
    public void testFindById(){

        Book book = bookDao.findById(20).get();
        System.out.println(book);
    }



@Test
    public void testFindByPageAndSort() throws IOException {

        // PageRequest.of()   構建的是一個Pageable對象,此對象是在spring-data-commons包下
        // 所以凡是spring data 系列,都可以用該對象來進行分頁
        Page<Book> books = bookDao.findAll(PageRequest.of(0, 10,Sort.Direction.ASC,"sales"));

        // 總條數
        long totalElements = books.getTotalElements();
        // 總頁數
        int totalPages = books.getTotalPages();
        System.out.println(totalElements);
        System.out.println(totalPages);

        books.forEach(book -> System.out.println(book));
    }

方法命名規則查詢

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BookDao extends ElasticsearchRepository<Book, Integer> {

    List<Book> findByContent(String content);

    List<Book> findByContentAndTitle(String content, String title);

    Page<Book> findByContent(String content, Pageable pageable);

}


@Test
    public void testMethodName(){

        List<Book> byContentAndTitle = bookDao.findByContentAndTitle("程序", "程序");

        byContentAndTitle.forEach(book -> System.out.println(book));
    }

springdata對es沒有封裝的方法

例如term、query_string、高亮顯示等,就用原生api

@Test
    public void  findQueryString(){
        //沒有封裝 的方法
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                //依舊傳遞的查詢方式  查詢參數
                .withQuery(QueryBuilders.queryStringQuery("我是程序員"))
                .build();


        Page<Book> page = bookDao.search(searchQuery);
        long totalElements = page.getTotalElements();
        System.out.println("總條數:"+totalElements);
        int totalPages = page.getTotalPages();
        System.out.println("總頁數:"+totalPages);
        List<Book> books = page.getContent();
        books.forEach(b-> System.out.println(b));

    }

    @Test
    public void  findTerm2(){
        //沒有封裝 的方法
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                //依舊傳遞的查詢方式  查詢參數
                .withQuery(QueryBuilders.termQuery("content","程序"))
                .build();


        Page<Book> page = bookDao.search(searchQuery);
        long totalElements = page.getTotalElements();
        System.out.println("總條數:"+totalElements);
        int totalPages = page.getTotalPages();
        System.out.println("總頁數:"+totalPages);
        List<Book> books = page.getContent();
        books.forEach(b-> System.out.println(b));

    }

高亮顯示

@Test
    public void testHighLight() {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                //依舊傳遞的查詢方式  查詢參數
                .withQuery(QueryBuilders.termQuery("content", "程序"))
                .withHighlightFields(new HighlightBuilder.Field("content").preTags("<xh style='color:red'>").postTags("</xh>"))
                .build();

        AggregatedPage<Book> page = elasticsearchTemplate.queryForPage(searchQuery, Book.class, new SearchResultMapper() {

            //自定義結果映射器 核心 將高亮字段取出 設置對象 返回數據就有高亮顯示 而spring-data-es 默認實現
            //DefaultResultMapper 它 不會取出高亮字段  不用
            @Override
            public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) {
                List<Book> books = new ArrayList<>();
                // 總條數
                long totalHits = searchResponse.getHits().getTotalHits();
                //System.out.println(totalHits);
                // 數據,包含高亮顯示的字段
                SearchHits hits = searchResponse.getHits();
                Iterator<SearchHit> iterator = hits.iterator();
                while (iterator.hasNext()) {
                    SearchHit sh = iterator.next();


                    // 每一條數據{id=17, title=程序員的自我修養—鏈接、裝載與庫, content=俯瞰程序前世今生參透代碼如何變成程序在系統中運行 透過系統軟件底層形成機制走進程序世界探索深層次的自己, sales=6856}
                    //高亮字段{content=[content], fragments[[俯瞰<xh style='color:red'>程序</xh>前世今生參透代碼如何變成<xh style='color:red'>程序</xh>在系統中運行 透過系統軟件底層形成機制走進<xh style='color:red'>程序</xh>世界探索深層次的自己]]}
                    Map<String, Object> source = sh.getSource();
                     System.out.println("每一條數據" + source);


                    Map<String, HighlightField> highlightFields = sh.getHighlightFields();
                     System.out.println("高亮字段" + highlightFields);

                    //開始封裝book對象

                    Book book = new Book();
                    Integer id = (Integer) source.get("id");
                    book.setId(id);
                    String title = (String) source.get("title");
                    book.setTitle(title);
                    HighlightField content = highlightFields.get("content");
                    book.setContent(content.getFragments()[0].toString());
                    Integer sales = (Integer) source.get("sales");
                    book.setSales(Long.valueOf(sales));

                    books.add(book);
                }

                return new AggregatedPageImpl(books, pageable, totalHits);
            }
        });


        long totalElements = page.getTotalElements();
        System.out.println("總條數:" + totalElements);
        int totalPages = page.getTotalPages();
        System.out.println("總頁數:" + totalPages);
        List<Book> books = page.getContent();
        books.forEach(b -> System.out.println(b));


    }

elasticsearch transport 通過9300操作

maven

<dependencies>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.6.8</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>5.6.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-to-slf4j</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.24</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.1</version>
        </dependency>
</dependencies>
// 創建客戶端對象
private TransportClient client;

    // 創建客戶端對象
    @Before
    public void init() {
        try {
            //創建一個客戶端對象
            Settings settings = Settings.builder()
                    .put("cluster.name", "my-elasticsearch")
                    .build();

            client = new PreBuiltTransportClient(settings)
                    //少服務器的地址
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
//                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9301))
//                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9302));

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }






// 創建type和mapping
client.admin().indices().preparePutMapping("test02")
                .setType("book")
                .setSource("{\n" +
                        "        \"book\": {\n" +
                        "            \"properties\": {\n" +
                        "                \"id\": {\n" +
                        "                \t\"type\": \"long\",\n" +
                        "                    \"store\": true,\n" +
                        "                    \"index\":\"not_analyzed\"\n" +
                        "                },\n" +
                        "                \"title\": {\n" +
                        "                \t\"type\": \"text\",\n" +
                        "                    \"store\": true,\n" +
                        "                    \"index\":\"analyzed\",\n" +
                        "                    \"analyzer\":\"ik_max_word\"\n" +
                        "                },\n" +
                        "                \"content\": {\n" +
                        "                \t\"type\": \"text\",\n" +
                        "                    \"store\": true,\n" +
                        "                    \"index\":\"analyzed\",\n" +
                        "                    \"analyzer\":\"ik_max_word\"\n" +
                        "                },\n" +
                        "                \"sales\":{\n" +
                        "                    \"type\": \"long\",\n" +
                        "                    \"store\": true,\n" +
                        "                    \"index\":\"not_analyzed\"\n" +
                        "                }\n" +
                        "            }\n" +
                        "        }\n" +
                        "    }", XContentType.JSON)
                .get();



// 文檔的crud
@Test
    public void testAdd(){

        client.prepareIndex("test02", "book", "1")
                .setSource(
                        "{\n" +
                                "\t\"id\":1,\n" +
                                "\t\"title\":\"測試添加\",\n" +
                                "\t\"content\":\"測試添加數據\",\n" +
                                "\t\"sales\":666\n" +
                                "}",XContentType.JSON
                )
                .get();

    }


// 實體類進行存儲
 @Test
    public void testAdd() throws JsonProcessingException {
        Book book = new Book();

        book.setId(2L);
        book.setTitle("對象測試");
        book.setContent("對象測試內容");
        book.setSales(1000L);

        // 使用json轉換工具
        ObjectMapper mappers = new ObjectMapper();

        String string = mappers.writeValueAsString(book);


        client.prepareIndex("test02", "book", "2")
                .setSource(
                        string,XContentType.JSON
                )
                .get();
    }



@Test
public void deleteDocument(){
    
	client.prepareDelete("test02", "book", "1").get();
	
}





// 批量導入
    @Test
    public void bulkAdd() throws IOException {

        BulkRequestBuilder bulkRequest = client.prepareBulk();

        // 數據在本地中,進行讀取
        File file = new File("F:\\darkHorse\\darkHorsePool\\springbootSeries\\dailyQuest\\day90_elasticSearch\\resource\\esData.txt");

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        String line = null;
        int i = 1000;

        while ((line = bufferedReader.readLine()) != null){
            bulkRequest.add(client.prepareIndex("test02", "book",i++ + "")
                    .setSource(line,XContentType.JSON)
            );
        }


        BulkResponse bulkResponse = bulkRequest.get();
        if (bulkResponse.hasFailures()) {
            // process failures by iterating through each bulk response item
        }
    }








 @Test
    public void testFindIds(){
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("test02")
                .setTypes("book")
                //這個地方 告訴構建器 使用什麼類型查詢方式
                //查詢方式 使用 QueryBuilders.term...matchall....queryString
                .setQuery(QueryBuilders.idsQuery().addIds("1000","1002","1003"))
                .setFrom(0)
                .setSize(20);


        //返回瞭一個 SearchResponse 響應對象
        SearchResponse searchResponse = searchRequestBuilder.get();


        SearchHits hits = searchResponse.getHits();

        long totalHits = hits.getTotalHits();
        System.out.println("一共多少條記錄:"+totalHits);

        Iterator<SearchHit> iterator = hits.iterator();

        while (iterator.hasNext()){
            SearchHit searchHit = iterator.next();

            Map<String, Object> source = searchHit.getSource();

            System.out.println(source);
        }
    }








SearchRequestBuilder searchRequestBuilder = client.prepareSearch("test02")
                .setTypes("book")
                //這個地方 告訴構建器 使用什麼類型查詢方式
                //查詢方式 使用 QueryBuilders.term...matchall....queryString
                .setQuery(QueryBuilders.termQuery("content","程序"))
                .setFrom(0)
                .setSize(20);









@Test
    public void highLight() {

        HighlightBuilder highlightBuilder = new HighlightBuilder()
                .preTags("<font style='color:red'>")
                .postTags("</font>")
                .field("content");

        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("test02")
                .setTypes("book")
                //這個地方 告訴構建器 使用什麼類型查詢方式
                //查詢方式 使用 QueryBuilders.term...matchall....queryString
                .setQuery(QueryBuilders.termQuery("content", "程序"))
                .setFrom(0)
                .setSize(20)
                .highlighter(highlightBuilder);


        //返回瞭一個 SearchResponse 響應對象
        SearchResponse searchResponse = searchRequestBuilder.get();


        SearchHits hits = searchResponse.getHits();

        long totalHits = hits.getTotalHits();
        System.out.println("一共多少條記錄:" + totalHits);

        Iterator<SearchHit> iterator = hits.iterator();

        while (iterator.hasNext()) {
            SearchHit searchHit = iterator.next();

            Map<String, Object> source = searchHit.getSource();

            System.out.println(source);

            //獲取高亮顯示的內容 這是一個map集合
            Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();

            HighlightField content = highlightFields.get("content");
            Text[] fragments = content.getFragments();
            System.out.println(fragments[0].toString());

        }

    }

以上就是Java實現調用ElasticSearch API的示例詳解的詳細內容,更多關於Java調用ElasticSearch API的資料請關註WalkonNet其它相關文章!

推薦閱讀: