elasticsearch JAVA增删改查

环境:IDEA下maven项目,jdk1.8    tomacat8.5    maven3.5

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">
    <modelVersion>4.0.0</modelVersion>
    <!--
        <groupId>反写的公司网址+项目名</groupId>
        <artifactId>项目名+模块名</artifactId>
        //第一位数字:代表大版本号
        第二位数字:代表分支版本号
        第三位数字:代表小版本号
        SNAPSHOT   快照版本
        ALPHA      内部版本
        BETA       公测
        RELEASE    稳定
        GA         正式发布
        <version>1.0-SNAPSHOT</version>
        //默认打包为jar,还可以是war,zip,pom
        <packaging></packaging>
        //项目描述名
        <name></name>
        //项目地址
        <url></url>
        //项目描述
        <description></description>
        //项目开发人员列表
        <developers></developers>
    -->
    <groupId>com.kakatadage</groupId>
    <artifactId>esdemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.44</version>
            <!--指定依赖范围,test测试范围有效
            compile默认范围,编译测试运行都有效
            provided测试编译有效
            runtime测试运行时有效
            system与本机系统相关联,可移植性差
            import导入范围,只在dependencyManagement中,表示从其他的pom导入dependency的配置-->
            <scope>test</scope>
            <!--<optional></optional>   设置依赖是否可选,默认为false-->
        </dependency>
    </dependencies>

    <build>
        <finalName>esdemo</finalName>
        <!--plugins设置插件的列表-->
        <plugins>
            <plugin>
                <!-- 设置javac编译器的版本和编码字符 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf8</encoding><!-- 编译器编码 -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

测试类文件:

package com.java.es.test;

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Map;

public class EsDemo {

    @Test
    /**
     * elasticsearch 与 JAVA 初级增删改查
     */
    public void test1() throws IOException {
        //指定ES集群
        Settings setting = Settings.builder().put("cluster.name", "docker-cluster").build();

        //创建访问ES服务器的客户端,host为服务器IP或者本地IP,取决于ES环境搭建的地方。host为String
        TransportClient client = new PreBuiltTransportClient(setting)
                .addTransportAddress(
                        new TransportAddress(
                                InetAddress.getByName(host),9300));
        //创建索引
//        IndexResponse response = client.prepareIndex("mfz", "mfz_tab", "2").setSource(XContentFactory.jsonBuilder()
//                .startObject().field("name", "gyz")
//                .field("sex", "男")
//                .field("age", "23")
//                .endObject()).get();
//        System.out.println("索引名称:" + response.getIndex() + "\n类型:" + response.getType()
//                + "\n文档ID:" + response.getId() + "\n当前实例状态:" + response.status());
        //修改索引
//        JSONObject jsonObject=new JSONObject();
//        jsonObject.put("name","mfz");
//        jsonObject.put("sex","男");
//        jsonObject.put("age","22");
//        UpdateResponse updateResponse = client.prepareUpdate("mfz", "mfz_tab", "1")
//                .setDoc(jsonObject.toString(), XContentType.JSON).get();
//        System.out.println("索引名称:" + updateResponse.getIndex() + "\n类型:" + updateResponse.getType()
//                + "\n文档ID:" + updateResponse.getId() + "\n当前实例状态:" + updateResponse.status());
//        //删除索引
//        DeleteResponse deleteResponse = client.prepareDelete("mfz", "mfz_tab", "2").get();
//        System.out.println("索引名称:" + deleteResponse.getIndex() + "\n类型:" + deleteResponse.getType()
//                + "\n文档ID:" + deleteResponse.getId() + "\n当前实例状态:" + deleteResponse.status());
        //获取索引
        GetResponse getResponse = client.prepareGet("mfz","mfz_tab","1").get();
        System.out.println("索引库的数据:" + getResponse.getSourceAsString());
        client.close();
    }
}

如果有问题,请参考:es(elasticsearch)初步学习总结(一),这篇是对第一篇的补充,第一篇中已经创建了一条文档

猜你喜欢

转载自blog.csdn.net/FV8023/article/details/97116519