(17) ElasticSearch query in java applications

  1, add clients rely elasticsearch

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

  Complete pom.xml

<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>com.edu</groupId>
    <artifactId>elasticSearch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>elasticSearch</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
View Code

  2, add log4j2.xml file, elasticsearch client uses log4j2日志, do not add runs will complain

  log4j2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appenders>
        <Console name="CONSOLE" target="system_out" follow="true">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level[%thread] %c [%L] -| %msg%n" />
        </Console>
    </appenders>
    <loggers>
        <root level="info">
            <appenderref ref="CONSOLE" />
        </root>
    </loggers>
</configuration>
View Code

  3, write the test class ESTest.java

package com.edu.elasticSearch;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

public class ESTest {

    @Test
    public void test1() throws UnknownHostException {
        
        //Es specified cluster 
        . Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application" ) .build (); 
        
        // create a client access server es 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                    .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // query index is lib3, type a user, id data 1 
        GetResponse response = client.prepareGet ( "lib3" , "user", ". 1" ) .execute () actionGet ();. 
        
        // get the data query 
        System.out.println (response.getSourceAsString ()); 
        client.close ();// close the client 
        
    } 
}
View Code

  4, start pages and client service es kibana, add test data

put /lib3/user/1
{
    "name":"zhaoliu",
    "address":"hei long jiang sheng tie ling shi",
    "age":50,
    "birthday":"1970-12-12",
    "interests":"xi huang hejiu,duanlian,lvyou"
}
View Code

  5, with the following results:

  The following points should be noted:

  (1) designated es cluster, also applies to a stand-alone configuration file elasticsearch.yml in cluster.name To cancel the annotation, as follows:

  (2) java specified port 9300, and elasticsearch.yml inconsistencies, as follows:

Guess you like

Origin www.cnblogs.com/javasl/p/12057382.html