package com.slm; import com.alibaba.fastjson.JSON; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.slm.model.ShopModel; import org.apache.http.HttpHost; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.*; public class ElasticsearchDemo { public static final String host = "192.168.199.246"; public static final int port = 9200; public static RestHighLevelClient client = null; public static String INDEX_NAME = "shop_test"; public static String INDEX_TYPE = "shop_type"; @Before public void init() { client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port))); } @After public void close() { if (client != null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port))); CreateIndexRequest request = new CreateIndexRequest("shop"); Map<String, Object> publicMap = new HashMap<String, Object>(); publicMap.put("name", "string"); publicMap.put("address", "text"); // publicMap.put("") request.mapping("public_shop", publicMap); request.settings(Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 2)); try { CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); client.close(); boolean acknowledged = createIndexResponse.isAcknowledged(); boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); System.out.println(acknowledged + ";" + shardsAcknowledged); } catch (IOException e) { e.printStackTrace(); } } @Test public void testExistIndex() { GetIndexRequest request = new GetIndexRequest(); request.indices("shop"); try { boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println("index : " + exists); } catch (IOException e) { e.printStackTrace(); } } @Test public void testGetIndex() { GetIndexRequest request = new GetIndexRequest(); request.indices("shop"); try { GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = getIndexResponse.getMappings(); System.out.println("mapping:"); Iterator<ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>>> iterator = mappings.iterator(); while (iterator.hasNext()) { ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> next = iterator.next(); System.out.println(next.key); System.out.println(next.index); Iterator<ObjectObjectCursor<String, MappingMetaData>> iterator1 = next.value.iterator(); while (iterator1.hasNext()) { ObjectObjectCursor<String, MappingMetaData> next1 = iterator1.next(); MappingMetaData value = next1.value; Map<String, Object> sourceAsMap = value.getSourceAsMap(); Collection<Object> values = sourceAsMap.values(); for (Object d : values) { System.out.println(":" + d.toString()); } } // String s = iterator.next().toString(); // System.out.println(s); System.out.println(); } String[] indices = getIndexResponse.getIndices(); System.out.println(Arrays.toString(indices)); } catch (IOException e) { e.printStackTrace(); } } @Test public void insertDoc() throws IOException { Map<String, Object> jsonMap = new HashMap<String, Object>(); jsonMap.put("title", "商家1"); jsonMap.put("address", "廣東省廣州市天河區天河東路111"); jsonMap.put("createTime", new Date()); jsonMap.put("name", "alias name"); for (int i = 0; i < 10; i++) { ShopModel shopModel = new ShopModel(); shopModel.setAddress("海外天地" +i); shopModel.setAddTime(new Date()); shopModel.setContent("可就是咖啡機啊" + i); shopModel.setId(i+1); shopModel.setShopId(i + 2); ArrayList<Double> objects = new ArrayList<Double>(); double tmp = i * 0.01; objects.add(47.432D + tmp); objects.add(127.223D + tmp); shopModel.setLocation(objects); shopModel.setTitle("酷酷酷酷酷" + i); IndexRequest request = new IndexRequest(INDEX_NAME, INDEX_TYPE, String.valueOf(shopModel.getId())) .source(JSON.toJSON(shopModel), XContentType.JSON); IndexResponse index = client.index(request, RequestOptions.DEFAULT); System.out.println(index.getId() + ": status:" + index.status().getStatus()); } } @Test public void testGetDoc() throws Exception { GetRequest request = new GetRequest(INDEX_NAME, INDEX_TYPE , "1"); GetResponse documentFields = client.get(request, RequestOptions.DEFAULT); System.out.println(documentFields.getSourceAsString()); } @Test public void searchDoc() throws Exception { SearchRequest request = new SearchRequest(); request.indices(INDEX_NAME); request.types(INDEX_TYPE); SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchQuery("content", "咖啡")); request.source(builder); //排序 SearchResponse search = client.search(request, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); SearchHit[] hits1 = hits.getHits(); System.out.println(Arrays.toString(hits1)); } @Test public void testExist() throws IOException { GetIndexRequest request = new GetIndexRequest(); request.indices(INDEX_NAME); request.types(INDEX_TYPE); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } public void testGetIdnex() throws Exception { GetIndexRequest request = new GetIndexRequest(); request.indices(INDEX_NAME); request.types(INDEX_TYPE); GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = getIndexResponse.getMappings(); Iterator<ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>>> iterator = mappings.iterator(); } @Test public void testCreateIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME); request.settings(Settings.builder() .put("number_of_shards", 2) .put("number_of_replicas", 1)); XContentBuilder builder = JsonXContent.contentBuilder() .startObject(). startObject("mappings") .startObject(INDEX_TYPE) .startObject("properties") .startObject("title") .field("type", "keyword") .endObject() .startObject("content") .field("type", "text") .endObject() .startObject("addTime") .field("type", "date") .endObject() .startObject("location") .field("type", "geo_point") .endObject() .startObject("shopId") .field("type", "integer") .endObject() .endObject() .endObject() .endObject() .endObject(); request.source(builder); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse.isAcknowledged()); } }