ElasticSearch基础(二):Java代码操作ElasticSearch


说明:

(1)这里使用Java操作ElasticSearch集群。

(2)Java使用TCP连接,使用9300类端口。


(1)pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>elasticSearch-demo</artifactId>
        <groupId>net.xiaof</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>es-client</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
    </properties>

    <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.25</version>
            <scope>test</scope>
        </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>
            <scope>test</scope>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

(2)实体:Article.java

package net.xiaof.entity;

import java.io.Serializable;

/**
 * @author zhangxh
 * @Description: Article实体
 * @date 2020-12-30
 */
public class Article implements Serializable {
    private Long id;
    private String title;
    private String content;

    public Article() {
    }

    public Article(long id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

(3)测试类:ElasticSearchClientTest.java

package net.xiaof.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.xiaof.entity.Article;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;


public class ElasticSearchClientTest {

    /**
     * (1)测试ES:创建索引(没有创建映射"mappings": { },)
     *
     * @throws Exception
     */
    @Test
    public void testCreateIndex() throws Exception {

        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        // client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        // client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));

        // 3.创建索引index_blog4(get方法执行操作)
        client.admin().indices().prepareCreate("index_blog1").get();

        // 4.释放资源
        client.close();
    }

    /**
     * (2)测试ES:给已存在索引设置映射mappings
     *
     * @throws Exception
     */
    @Test
    public void testCreateIndexAndMapping() throws Exception {

        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        // client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        // client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));

        // 3.创建映射mapping
        /*
        {
          "mappings": {
            "article": {
              "properties": {
                "id": {
                  "type": "long",
                  "store": true,
                  "index": "not_analyzed"
                },
                "title": {
                  "type": "text",
                  "store": true,
                  "index": "analyzed",
                  "analyzer": "ik_smart"
                },
                "content": {
                  "type": "text",
                  "store": true,
                  "index": "analyzed",
                  "analyzer": "ik_smart"
                }
              }
            }
          }
        }
         */
        // (3.1)构建mapping信息
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("article")
                .startObject("properties")
                .startObject("id")
                .field("type", "long").field("store", "yes")
                .endObject()
                .startObject("title")
                .field("type", "string").field("store", "yes").field("analyzer", "ik_smart")
                .endObject()
                .startObject("content")
                .field("type", "string").field("store", "yes").field("analyzer", "ik_smart")
                .endObject()
                .endObject()
                .endObject()
                .endObject();
        // (3.2)将mapping对象添加到索引index_blog1中(前提:索引index_blog1必须已经创建)
        PutMappingRequest mapping = Requests.putMappingRequest("index_blog1").type("article").source(builder);
        // (3.3)执行设置mapping(get方法执行操作)
        client.admin().indices().putMapping(mapping).get();

        // 4.释放资源
        client.close();
    }

    /**
     * (3)测试ES:给已存在索引创建文档(通过XContentBuilder)
     *
     * @throws Exception
     */
    @Test
    public void testCreateDocument() throws Exception {

        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.创建文档,档json:{"id": 1,"title": "","content": ""}
        // (3.1)构建文档信息
        String title = "ElasticSearch是一个基于Lucene的搜索服务器";
        String content = "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。";
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .field("id", 1).field("title", title).field("content", content)
                .endObject();
        // (3.2)创建文档(get方法执行操作)
        /**
         * 【API说明】:
         * IndexRequestBuilder prepareIndex(String index, String type, @Nullable String id)
         * 参数:
         * String index:索引
         * String type:类型
         * String id:id
         */
        client.prepareIndex("index_blog1", "article", "1").setSource(builder).get();

        // 4.释放资源
        client.close();
    }

    /**
     * (4)测试ES:给已存在索引创建文档(使用Jackson转换实体)
     *
     * @throws Exception
     */
    @Test
    public void testCreateDocument2() throws Exception {

        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.创建文档
        // 文档json:{"id": 1,"title": "","content": ""}
        // (3.1)构建文档对象
        String title = "(2)ElasticSearch是一个基于Lucene的搜索服务器";
        String content = "(2)它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。";
        Article article = new Article(2, title, content);
        // (3.2)执行创建文档(使用jackson工具)
        ObjectMapper objectMapper = new ObjectMapper();
        client.prepareIndex("index_blog1", "article", article.getId().toString())
                .setSource(objectMapper.writeValueAsString(article).getBytes(), XContentType.JSON).get();

        // 4.释放资源
        client.close();
    }

    /**
     * (5)测试ES:删除文档(通过id)
     *
     * @throws Exception
     */
    @Test
    public void testDelDocument() throws Exception {
        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.删除文档(通过id)
        client.prepareDelete("index_blog1","article","100").get();

        // 4.释放资源
        client.close();
    }

    /**
     * (6)测试ES:查询文档(通过关键词Term查询)
     *
     * @throws Exception
     */
    @Test
    public void testGetDocument1() throws Exception {

        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.查询操作
        // (3.1)设置搜索条件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "搜索");
        SearchResponse searchResponse = client.prepareSearch("index_blog1")
                .setTypes("article").setQuery(termQueryBuilder).get();
        //(3.2)处理搜索结果
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询结果有:" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            SearchHit searchHit = iterator.next();
            //打印文档json字符串
            System.out.println("【文档json字符串】-------------------");
            System.out.println(searchHit.getSourceAsString());
            //获取文档的属性
            System.out.println("【文档属性】=========================");
            Map<String, Object> document = searchHit.getSource();
            System.out.println("id:" + document.get("id"));
            System.out.println("title:" + document.get("title"));
            System.out.println("content:" + document.get("content"));
        }

        // 4.释放资源
        client.close();
    }

    /**
     * (7)测试ES:查询文档(通过字符串查询)
     *
     * @throws Exception
     */
    @Test
    public void testGetDocument2() throws Exception {

        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.查询操作
        // (3.1)设置搜索条件
        SearchResponse searchResponse = client.prepareSearch("index_blog1")
                .setTypes("article")
                .setQuery(QueryBuilders.queryStringQuery("搜索")).get();
        //(3.2)处理搜索结果
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询结果有:" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            SearchHit searchHit = iterator.next();
            //打印文档json字符串
            System.out.println("【文档json字符串】-------------------");
            System.out.println(searchHit.getSourceAsString());
            //获取文档的属性
            System.out.println("【文档属性】=========================");
            Map<String, Object> document = searchHit.getSource();
            System.out.println("id:" + document.get("id"));
            System.out.println("title:" + document.get("title"));
            System.out.println("content:" + document.get("content"));
        }

        // 4.释放资源
        client.close();
    }

    /**
     * (8)测试ES:查询文档(通过文档id查询)
     *
     * @throws Exception
     */
    @Test
    public void testGetDocument3() throws Exception {

        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.查询操作
        // (3.1)设置搜索条件
        SearchResponse searchResponse = client.prepareSearch("index_blog1")
                .setTypes("article")
                .setQuery(QueryBuilders.idsQuery().addIds("2")).get();//查询id为2的文档(也可一次查询多条【.addIds("1", "2")】)
        //(3.2)处理搜索结果
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询结果有:" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            SearchHit searchHit = iterator.next();
            //打印文档json字符串
            System.out.println("【文档json字符串】-------------------");
            System.out.println(searchHit.getSourceAsString());
            //获取文档的属性
            System.out.println("【文档属性】=========================");
            Map<String, Object> document = searchHit.getSource();
            System.out.println("id:" + document.get("id"));
            System.out.println("title:" + document.get("title"));
            System.out.println("content:" + document.get("content"));
        }

        // 4.释放资源
        client.close();
    }

    /**
     * (9)测试ES:批量插入数据(使用Jackson转换实体)
     *
     * @throws Exception
     */
    @Test
    public void testBatchInsert() throws Exception {
        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.创建文档
        // 文档json:{"id": 1,"title": "","content": ""}
        String title = "ElasticSearch是一个基于Lucene的搜索服务器";
        String content = "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。";
        Article article = null;
        ObjectMapper objectMapper = new ObjectMapper();
        for (int i = 1; i <= 100; i++) {
            // (3.1)构建文档对象
            article = new Article(i, "("+i+")"+title, "("+i+")"+content);
            // (3.2)执行创建文档(使用jackson工具)
            client.prepareIndex("index_blog1", "article", article.getId().toString())
                    .setSource(objectMapper.writeValueAsString(article).getBytes(), XContentType.JSON).get();
        }

        // 4.释放资源
        client.close();
    }

    /**
     * (10)测试ES:分页查询
     *
     * @throws Exception
     */
    @Test
    public void testGetDataPaging() throws Exception {
        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.分页查询
        // (3.1)设置查询条件
        // QueryBuilder queryBuilder = QueryBuilders.termQuery("title", "搜索");//关键词查询
        // MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();//查询所有
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "搜索");
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("index_blog1")
                .setTypes("article").setQuery(termQueryBuilder);
        // (3.2)默认每页10条(这里设置:从0开始,每页5条)
        SearchResponse searchResponse = searchRequestBuilder.setFrom(0).setSize(5).get();
        // (3.3)处理查询结果
        SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
        System.out.println("查询结果有:" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            SearchHit searchHit = iterator.next(); // 每个查询对象
            System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印
            System.out.println("id:" + searchHit.getSource().get("id"));
            System.out.println("title:" + searchHit.getSource().get("title"));
            System.out.println("content:" + searchHit.getSource().get("content"));
            System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐");
        }

        // 4.释放资源
        client.close();
    }

    /**
     * (11)测试ES:查询结果高亮显示(对es查询出的内容中关键字部分进行标签和样式的设置)
     *
     * @throws Exception
     */
    @Test
    public void testGetHighlight() throws Exception {
        // 1.创建settings配置对象(配置集群信息)
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();

        // 2.创建Client连接对象(TCP连接,这里使用9300类端口;http使用9200类端口;为保障可用,下面添加多个集群节点端口,一个也行)
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        // 3.查询
        //(3.1)设置搜索条件
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("index_blog1")
                .setTypes("article").setQuery(QueryBuilders.termQuery("title", "搜索"));
        //(3.2)设置高亮显示数据
        HighlightBuilder hiBuilder=new HighlightBuilder();
        hiBuilder.preTags("<font style='color:red'>");
        hiBuilder.postTags("</font>");
        hiBuilder.field("title");
        searchRequestBuilder.highlighter(hiBuilder);
        //(3.3)获取并处理结果数据
        SearchResponse searchResponse = searchRequestBuilder.get();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println("共搜到:"+searchHits.getTotalHits()+"条结果!");
        System.out.println("********************************");
        //遍历结果
        for(SearchHit hit:searchHits){
            System.out.println("String方式打印文档搜索内容:"+hit.getSourceAsString());
            System.out.println("Map方式打印高亮内容:"+hit.getHighlightFields());
            System.out.println("遍历高亮集合,打印高亮片段:");
            Text[] text = hit.getHighlightFields().get("title").getFragments();
            for (Text str : text) {
                System.out.println(str);
            }
            System.out.println("----------------------------------------");
        }
        System.out.println("**************************************");

        // 4.释放资源
        client.close();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值