ElasticSearch学习(二)

在java项目中的应用

版本6.7.0 一定要注意版本

一、pom依赖

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <!--es引入-->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>6.7.0</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>6.7.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.8</version>
    </dependency>

</dependencies>

<build>
    <finalName>es</finalName>
    <resources>
        <resource>
            <targetPath>${project.build.directory}/classes</targetPath>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <!-- 结合com.alibaba.dubbo.container.Main -->
        <resource>
            <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
            <directory>src/main/resources/spring</directory>
            <filtering>true</filtering>
            <includes>
                <include>spring-context.xml</include>
            </includes>
        </resource>
    </resources>

    <pluginManagement>
        <plugins>
            <!-- 解决Maven插件在Eclipse内执行了一系列的生命周期引起冲突 -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <versionRange>[2.0,)</versionRange>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <classesDirectory>target/classes/</classesDirectory>
                <archive>
                    <manifest>
                        <mainClass>com.alibaba.dubbo.container.Main</mainClass>
                        <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
                        <useUniqueVersions>false</useUniqueVersions>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <type>jar</type>
                        <includeTypes>jar</includeTypes>
                        <!--<useUniqueVersions>false</useUniqueVersions>-->
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

二、代码

1.创建getClient连接

// 2.3.0 的创建

TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.107"), 9300));

// 6.7.0 的创建

public final static String HOST = "127.0.0.1";
// http请求的端口是9200,客户端是9300
public final static int PORT = 9300;
// 设置集群名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
// 创建client
TransportClient client = new PreBuiltTransportClient(settings)
        .addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));

发现报错:

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{nPTGVupcS1yN3AibDPWIWA}{127.0.0.1}{127.0.0.1:9300}] ]

解决方法:找到主机的elasticsearch.yml文件 添加设置

cluster.name: my-application
xpack.security.transport.filter.allow: 127.0.0.1
xpack.security.enabled: false
xpack.security.transport.ssl.enabled: false
xpack.security.http.ssl.enabled: false

*注意cluster.name必须一致

参考博客:https://subhasishsahu.wordpress.com/2017/07/05/elastic-search-5-4-with-java-client/

2.创建索引文件

public static void createIndexFile() throws Exception{
    XContentBuilder mapping  = XContentFactory.jsonBuilder()
            .startObject()
                .startObject("settings")
                    .field("number_of_shards", 1)
                    .field("number_of_replicas", 0)
                .endObject()
            .endObject()
            .startObject()
                .startObject("book")
                    .startObject("properties")
                        .startObject("BookId")
                            .field("type", "Integer")
                            .field("store", "yes")
                        .endObject()
                        .startObject("BookName")
                            .field("type", "String")
                            .field("store", "yes")
                        .endObject()
                        .startObject("BookPrice")
                            .field("type","Integer")
                            .field("format", "dateOptionalTime")
                            .field("store", "yes")
                        .endObject()
                        .startObject("BookIntroduction")
                            .field("type","string")
                            .field("index", "not_analyzed")
                            .field("store", "yes")
                        .endObject()
                    .endObject()
                .endObject()
            .endObject();
    CreateIndexRequestBuilder builder = getClient().admin().indices().prepareCreate("book").setSource(mapping);
    CreateIndexResponse response = builder.execute().actionGet();
    if(response.isAcknowledged()){
        System.out.println("创建索引文档成功!");
    }else{
        System.out.println("创建索引文档失败!");
    }
}

3.增加文档

以实体类转换为json格式增加文档

index_name:book

index_type:math

index_id:3

*注意:index_name必须小写且不可重复

public static void addIndexFile() throws Exception{
    ObjectMapper mapper = new ObjectMapper();
    Book book = new Book(3, "Math Formula", 29, "none");
    byte[] json = mapper.writeValueAsBytes(book);
    IndexResponse response = getClient().prepareIndex("book", "math", "3")
            .setSource(json, XContentType.JSON).get();
    System.out.println("index: " + response.getIndex() + " insert doc id: " + response.getId());
}

4.修改文档

public static void updateIndexFile() throws Exception{
    ObjectMapper mapper = new ObjectMapper();
    byte[] json = mapper.writeValueAsBytes(new Book(3, "Math Formula1", 69, "none"));
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("book");
    updateRequest.type("math");
    updateRequest.id("3");
    updateRequest.doc(json, XContentType.JSON);
    System.out.println(getClient().update(updateRequest).get());
}

5.查询文档

public static void queryIndexFile() throws Exception{
    GetResponse response = getClient().prepareGet("book", "math", "1").get();
    String source = response.getSource().toString();
    long version = response.getVersion();
    String indexName = response.getIndex();
    String type = response.getType();
    String id = response.getId();

    System.out.println("source===>>" + source);
    System.out.println("version===>>" + version);
    System.out.println("indexName===>>" + indexName);
    System.out.println("type===>>" + type);
    System.out.println("id===>>" + id);
}

6.删除类型化的JSON文档

public static void deleteIndexFile() throws Exception{
    DeleteResponse response = getClient().prepareDelete("index_name_second", "type_name_second", "1").get();
    System.out.println(response.getIndex());  
}

三、完整代码

import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

public class BookIndexFileUtils {
    public final static String HOST = "127.0.0.1";
    public final static int PORT = 9300;

    /**
     * 获取client句柄
     * @return
     * @throws Exception
     */
    private static Client getClient() throws Exception{
        // 设置集群名称
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();
        // 创建client
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));
        return client;
    }

    /**
     * 创建索引文件
     */
    public static void createIndexFile() throws Exception{
        XContentBuilder mapping  = XContentFactory.jsonBuilder()
                .startObject()
                    .startObject("settings")
                        .field("number_of_shards", 1)
                        .field("number_of_replicas", 0)
                    .endObject()
                .endObject()
                .startObject()
                    .startObject("book")
                        .startObject("properties")
                            .startObject("BookId")
                                .field("type", "Integer")
                                .field("store", "yes")
                            .endObject()
                            .startObject("BookName")
                                .field("type", "String")
                                .field("store", "yes")
                            .endObject()
                            .startObject("BookPrice")
                                .field("type","Integer")
                                .field("format", "dateOptionalTime")
                                .field("store", "yes")
                            .endObject()
                            .startObject("BookIntroduction")
                                .field("type","string")
                                .field("index", "not_analyzed")
                                .field("store", "yes")
                            .endObject()
                        .endObject()
                    .endObject()
                .endObject();
        CreateIndexRequestBuilder builder = getClient().admin().indices().prepareCreate("book").setSource(mapping);
        CreateIndexResponse response = builder.execute().actionGet();
        if(response.isAcknowledged()){
            System.out.println("创建索引文档成功!");
        }else{
            System.out.println("创建索引文档失败!");
        }
    }

    /**
     * 增加文档
     * @throws Exception
     */
    public static void addIndexFile() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        Book book = new Book(3, "Math Formula", 29, "none");
        byte[] json = mapper.writeValueAsBytes(book);
        IndexResponse response = getClient().prepareIndex("book", "math", "3")
                .setSource(json, XContentType.JSON).get();
        System.out.println("index: " + response.getIndex() + " insert doc id: " + response.getId());
    }


    /**
     * 修改文档
     * @throws Exception
     */
    public static void updateIndexFile() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        byte[] json = mapper.writeValueAsBytes(new Book(3, "Math Formula1", 69, "none"));
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("book");
        updateRequest.type("math");
        updateRequest.id("3");
        updateRequest.doc(json, XContentType.JSON);
        System.out.println(getClient().update(updateRequest).get());
    }


    /**
     * 查询文档
     * @throws Exception
     */
    public static void queryIndexFile() throws Exception{
        GetResponse response = getClient().prepareGet("book", "math", "1").get();
        String source = response.getSource().toString();
        long version = response.getVersion();
        String indexName = response.getIndex();
        String type = response.getType();
        String id = response.getId();

        System.out.println("source===>>" + source);
        System.out.println("version===>>" + version);
        System.out.println("indexName===>>" + indexName);
        System.out.println("type===>>" + type);
        System.out.println("id===>>" + id);
    }


    /**
     * 删除类型化的JSON文档
     * @throws Exception
     */
    public static void deleteIndexFile() throws Exception{
        DeleteResponse response = getClient().prepareDelete("index_name_second", "type_name_second", "1").get();
        System.out.println(response.getIndex());
    }
}

四、报错

java.lang.NoSuchFieldError: LUCENE_4_0_0

    at org.elasticsearch.Version.<clinit>(Version.java:44)
    at org.elasticsearch.common.io.stream.StreamOutput.<init>(StreamOutput.java:56)
    at org.elasticsearch.common.io.stream.BytesStreamOutput.<init>(BytesStreamOutput.java:60)
    at org.elasticsearch.common.io.stream.BytesStreamOutput.<init>(BytesStreamOutput.java:57)
    at org.elasticsearch.common.io.stream.BytesStreamOutput.<init>(BytesStreamOutput.java:47)
    at org.elasticsearch.common.xcontent.XContentBuilder.builder(XContentBuilder.java:81)
    at org.elasticsearch.common.xcontent.json.JsonXContent.contentBuilder(JsonXContent.java:38)
    at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:122)
    at org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder(XContentFactory.java:49)
    at es.IndexFileUtils.createIndexFile(IndexFileUtils.java:42)
    at es.IndexFileTest.testCreateIndexFile(IndexFileTest.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

与lucene的版本冲突

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值