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</groupId>
<artifactId>TestES</artifactId>
<version>1.0-SNAPSHOT</version>
<name>es-core-first</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
</project>
CURD 增删该查
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.elasticsearch.common.transport.TransportAddress;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class TestESCURD {
public static void main(String[] args) {
Settings settings = Settings.builder()
.put("cluster.name", "elasticsearch")
.build();
try {
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("sa0"),9300));
// CreateEmployee(client);
// UpdateEmployee(client);
// getEmployee(client);
deleteEmployee(client);
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
// 增加
public static void CreateEmployee(TransportClient client) {
try {
IndexResponse response = client.prepareIndex("company","employee", "1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name","jack")
.field("age",27)
.field("position","technique")
.field("country","china")
.field("join_date","2017-01-01")
.field("slary",10000)
.endObject()).get();
System.out.println(response.getResult());
} catch (IOException e) {
e.printStackTrace();
}
}
// 查询
public static void getEmployee(TransportClient client) {
GetResponse response = client.prepareGet("company","employee","1").get();
System.out.println(response.getSourceAsString());
}
// 更新
public static void UpdateEmployee(TransportClient client) {
UpdateResponse response = null;
try {
response = client.prepareUpdate("company", "employee", "1")
.setDoc(XContentFactory.jsonBuilder()
.startObject()
.field("position","techine manger")
.endObject()).get();
System.out.println(response.getResult());
} catch (IOException e) {
e.printStackTrace();
}
}
// 删除
public static void deleteEmployee(TransportClient client) {
DeleteResponse response = client.prepareDelete("company", "employee", "1").get();
System.out.println(response.getResult());
}
}
查询
import org.elasticsearch.action.search.SearchResponse;
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.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class EmployeeSearchApp {
public static void main(String[] args) {
Settings settings = Settings.builder()
.put("cluster.name","elasticsearch")
.build();
try {
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("sa0"),9300));
// prepareData(client);
executeSearch(client);
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void executeSearch(TransportClient client) {
SearchResponse response = client.prepareSearch("company")
.setQuery(QueryBuilders.matchQuery("position", "technique"))
.setPostFilter(QueryBuilders.rangeQuery("age").from(30).to(40))
.setFrom(0).setSize(1)
.get();
SearchHit[] searchHits = response.getHits().getHits();
for( int i = 0; i < searchHits.length; i++ ) {
System.out.println(searchHits[i].getSourceAsString());
}
}
public static void prepareData(TransportClient client) throws Exception {
client.prepareIndex("company", "employee", "1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name", "jack")
.field("age", 27)
.field("position", "technique software")
.field("country", "china")
.field("join_date", "2017-01-01")
.field("salary", 10000)
.endObject())
.get();
client.prepareIndex("company", "employee", "2")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name", "marry")
.field("age", 35)
.field("position", "technique manager")
.field("country", "china")
.field("join_date", "2017-01-01")
.field("salary", 12000)
.endObject())
.get();
client.prepareIndex("company", "employee", "3")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name", "tom")
.field("age", 32)
.field("position", "senior technique software")
.field("country", "china")
.field("join_date", "2016-01-01")
.field("salary", 11000)
.endObject())
.get();
client.prepareIndex("company", "employee", "4")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name", "jen")
.field("age", 25)
.field("position", "junior finance")
.field("country", "usa")
.field("join_date", "2016-01-01")
.field("salary", 7000)
.endObject())
.get();
client.prepareIndex("company", "employee", "5")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("name", "mike")
.field("age", 37)
.field("position", "finance manager")
.field("country", "usa")
.field("join_date", "2015-01-01")
.field("salary", 15000)
.endObject())
.get();
}
}
实例:
Settings settings = ImmutableSettings
.settingsBuilder()
.put("cluster.name", "elasticsearch")
.build();
TransportClient client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.253"),9300));
GetResponse response = client.prepareGet("qx_day", "qx_day", "45853483780199").get();
System.out.println(response.getSourceAsString());
SearchResponse searchResponse = client.prepareSearch("qx_day").get();
SearchHit[] searchHits = searchResponse.getHits().getHits();
for ( int i = 0; i < searchHits.length; i++ ) {
System.out.println(searchHits[i].getSourceAsString());
}
精确匹配过滤
public static void SearchByReq() {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch").build();
TransportClient client = null;
client = new TransportClient(settings);
try {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.253"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("stationid", "53983");
SearchRequestBuilder searchRequestBuilder = null;
searchRequestBuilder = client.prepareSearch("qx_file").setTypes("qx_file").setSearchType(SearchType.QUERY_THEN_FETCH);
searchRequestBuilder.setQuery(queryBuilder);
SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
SearchHit[] hitArray = searchRespons.getHits().getHits();
for( int i = 0; i < hitArray.length; i++) {
System.out.println(hitArray[i].id());
}
}
按照范围过滤
// 按照起始条件和结束条件查询
public static void SearchByReq1() {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch").build();
TransportClient client = null;
client = new TransportClient(settings);
try {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.253"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
SearchRequestBuilder searchRequestBuilder = null;
searchRequestBuilder = client.prepareSearch("qx_file").setTypes("qx_file").setSearchType(SearchType.QUERY_THEN_FETCH);
QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("stationid").gt("03547");
QueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("stationid").lt("53889");
searchRequestBuilder.setQuery(queryBuilder1).setQuery(queryBuilder2);
SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
SearchHit[] hitArray = searchRespons.getHits().getHits();
for( int i = 0; i < hitArray.length; i++) {
System.out.println(hitArray[i].id());
System.out.println(hitArray[i].sourceAsMap());
}
}