1. 程式人生 > >ElasticSearch基本操作

ElasticSearch基本操作

1.建立一個maven專案,編寫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.immo</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>5.6.2</version>
		</dependency>

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

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2,編寫demo類:

package com.immo.elasticsearch;

import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetRequestBuilder;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ElasticSearchDemo {
	private Client client;

	/**
	 * 獲取連線
	 * 
	 * @throws Exception
	 */
	@Before
	public void getClient() throws Exception {
		Settings settings1 = Settings.builder().put("cluster.name", "my-elasticsearch")
				.put("client.transport.sniff", true).build();
		client = new PreBuiltTransportClient(Settings.EMPTY)
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
	}

	/**
	 * 關閉連線
	 */
	@After
	public void closeClose() {
		client.close();
	}

	/**
	 * 建立索引
	 * 
	 * @throws Exception
	 */
	@Test
	public void createIndex() throws Exception {
		for (int i = 0; i < 100; i++) {

			/**
			 * 第一種
			 */
			/*
			 * XContentBuilder source =
			 * XContentFactory.jsonBuilder().startObject() .field("title", "蘋果"
			 * + i) .field("releaseTime", new Date()) .field("price", i * 0.7 *
			 * 5888) .field("message", "這是剛剛釋出的蘋果手機,準備好腎!!!") .endObject();
			 */
			/**
			 * 這是第二種,還可以傳入json和實體類,不一一演示了
			 */
			Map<String, Object> source = new HashMap();
			source.put("title", "蘋果" + i);
			source.put("releaseTime", new Date());
			source.put("price", i * 0.7 * 5888);
			source.put("message", "這是剛剛釋出的蘋果手機,準備好腎!!!");

			// 存json入索引中,setSource可以放入map,或者實體,或者json,能表現key:value形式就好
			IndexResponse response = client.prepareIndex("product", "mobile", "apple" + i).setSource(source).get();
			// 結果獲取
			String index = response.getIndex();
			String type = response.getType();
			String id = response.getId();
			long version = response.getVersion();
			System.out.println(index + " : " + type + ": " + id + ": " + version);
		}
	}

	/**
	 * 搜尋索引,精準查詢
	 */
	@Test
	public void selectIndexById() {
		for (int i = 0; i < 100; i++) {
			// GetResponse response = client.prepareGet("product", "mobile",
			// "apple1")
			// .get();
			GetResponse response = client.prepareGet("product", "mobile", "apple" + i).setOperationThreaded(false) // 執行緒安全
					.get();
			System.out.println(response.getSourceAsString());
		}
	}

	/**
	 * 按照條件查詢
	 * 
	 * @param queryBuilder
	 * @param indexname
	 * @param type
	 */
	@Test
	public void searcher2() {
		// QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("title", "22");
		// QueryBuilder queryBuilder =
		// QueryBuilders.matchQuery("title","果1");//匹配模式,會把查詢的引數分詞
		QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("kimchy", "title", "user", "status"); // title,或者title或者user或者status包含kimchy的資料

		// http://blog.csdn.net/xiaohulunb/article/details/37877435

		SearchResponse searchResponse = client.prepareSearch("product").setTypes("mobile").setQuery(queryBuilder)
				.execute().actionGet();
		SearchHits hits = searchResponse.getHits();
		System.out.println("查詢到記錄數=" + hits.getTotalHits());
		SearchHit[] searchHists = hits.getHits();
		if (searchHists.length > 0) {
			for (SearchHit hit : searchHists) {
				Double price = (Double) hit.getSource().get("price");
				String message = (String) hit.getSource().get("message");
				String title = (String) hit.getSource().get("title");
				String id = (String) hit.getId();
				System.out.println(price + "   " + message + "   " + title + "  " + id);
			}
		}
	}

	/**
	 * 刪除索引,根據id刪除
	 */
	@Test
	public void deleteIndex() {
		DeleteResponse response = client.prepareDelete("product", "mobile", "apple1").get();
		String index = response.getIndex();
		String type = response.getType();
		String id = response.getId();
		long version = response.getVersion();
		System.out.println(index + " : " + type + ": " + id + ": " + version);
	}

	/**
	 * 跟新索引,使用updateRequest物件
	 * 
	 * @throws Exception
	 */
	@Test
	public void updateIndex() throws Exception {
		UpdateRequest updateRequest = new UpdateRequest();
		updateRequest.index("product");
		updateRequest.type("mobile");
		updateRequest.id("apple16");
		updateRequest.doc(XContentFactory.jsonBuilder().startObject()
				// 對沒有的欄位新增, 對已有的欄位替換
				.field("status", "1").field("message", "hello").endObject());
		UpdateResponse response = client.update(updateRequest).get();

		// 列印
		String index = response.getIndex();
		String type = response.getType();
		String id = response.getId();
		long version = response.getVersion();
		System.out.println(index + " : " + type + ": " + id + ": " + version);
	}

	/**
	 * 測試update api, 使用client
	 * 
	 * @throws Exception
	 */
	@Test
	public void updateIndex2() throws Exception {
		// 使用Script物件進行更新
		// UpdateResponse response = client.prepareUpdate("product", "mobile",
		// "apple16")
		// .setScript(new Script("hits._source.status = \"male\""))
		// .get();

		// 使用XContFactory.jsonBuilder() 進行更新
		// UpdateResponse response = client.prepareUpdate("product", "mobile",
		// "apple16")
		// .setDoc(XContentFactory.jsonBuilder()
		// .startObject()
		// .field("status", "malelelele")
		// .endObject()).get();

		// 使用updateRequest物件及script
		// UpdateRequest updateRequest = new UpdateRequest("product", "mobile",
		// "apple16")
		// .script(new Script("ctx._source.status=\"male\""));
		// UpdateResponse response = client.update(updateRequest).get();

		// 使用updateRequest物件及documents進行更新
		UpdateResponse response = client.update(new UpdateRequest("product", "mobile", "apple16")
				.doc(XContentFactory.jsonBuilder().startObject().field("status", "100").endObject())).get();
		System.out.println(response.getIndex());
	}

	/**
	 * 測試update 使用updateRequest
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	@Test
	public void updateIndex3() throws Exception {
		UpdateRequest updateRequest = new UpdateRequest("product", "mobile", "apple16")
				.script(new Script("ctx._source.status=\"112\""));
		UpdateResponse response = client.update(updateRequest).get();
	}

	/**
	 * 測試upsert方法--更新的時候發現沒有這條資料,則插入上面定義的資料,但是所在域和型別和id還是UpdateRequest方法裡面設定的那個
	 * 
	 * @throws Exception
	 * 
	 */
	@Test
	public void updateIndex4() throws Exception {
		// 設定查詢條件, 查詢不到則新增生效
		IndexRequest indexRequest = new IndexRequest("product1", "mobile1", "apple1116").source(XContentFactory
				.jsonBuilder().startObject().field("name", "qergef").field("gender", "malfdsae").endObject());
		// 設定更新, 查詢到更新下面的設定
		UpdateRequest upsert = new UpdateRequest("product", "mobile", "apple1112")
				.doc(XContentFactory.jsonBuilder().startObject().field("user", "wenbronk").endObject())
				.upsert(indexRequest);

		client.update(upsert).get();
	}

	/**
	 * 測試multi get api 從不同的index, type, 和id中獲取
	 */
	@Test
	public void testMultiGet() {
		MultiGetRequestBuilder prepareMultiGet = client.prepareMultiGet();
		MultiGetResponse multiGetResponse = prepareMultiGet.add("product", "mobile", "apple16", "apple17", "apple18")
				.add("product2", "mobile", "apple16", "apple17", "apple18").add("product", "mobile2", "apple16").get();

		for (MultiGetItemResponse itemResponse : multiGetResponse) {
			GetResponse response = itemResponse.getResponse();
			if (response.isExists()) {
				String sourceAsString = response.getSourceAsString();
				System.out.println(sourceAsString);
			}
		}
	}

	/**
	 * bulk 批量執行 一次查詢可以update 或 delete多個document
	 */
	@Test
	public void testBulk() throws Exception {
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		bulkRequest
				.add(client.prepareIndex("twitter", "tweet", "1")
						.setSource(XContentFactory.jsonBuilder().startObject().field("user", "kimchy")
								.field("postDate", new Date()).field("message", "trying out Elasticsearch")
								.endObject()));
		bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
				.setSource(XContentFactory.jsonBuilder().startObject().field("user", "kimchy")
						.field("postDate", new Date()).field("message", "another post").endObject()));
		BulkResponse response = bulkRequest.get();
	}
}