学习文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
下载地址:https://www.elastic.co/downloads/past-releases/elasticsearch-2-3-1
安装head插件,在网页上管理、监视集群的状态。bin目录下plugin install mobz/elasticsearch-head
访问地址:http://localhost:9200/_plugin/head/
创建索引(Indices)
curl -XPOST localhost:9200/test -d '{
"settings":{
"number_of_shards":3,
"number_of_replicas":2
},
"mappings":{
"user":{
"properties":{
"ID":{
"type":"string",
"index":"not_analyzed"
},
"NAME":{
"type":"string",
"fields":{
"NAME":{
"type":"string"
},
"raw":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
}
}
}'
删除api 以根据特定的ID删除文档
curl -XDELETE 'http://localhost:9200/test/user/1'
//增加单条索引 这里的100对应于_id字段
curl -XPUT 'http://localhost:9200/test/user/100' -d '{
"id" : 1,
"name" : "yuanhai",
"age" : "30"
}'
用自定义的Routing模式, 不需要向所有分片发送请求,会大大减少系统资源的浪费。
1.直接在请求的URL中指定routing参数:
curl -XPOST 'http://localhost:9200/test/student?routing=good' -d '
{
"ID": "1",
"NAME": "yuanhai"
}'
2.第二种方法就是直接从文档中提取到对应的路由值:(/_mapping相当于?op_type=mapping)
curl -XPUT 'http://localhost:9200/store/order/_mapping' -d '
{
"order": {
"_routing": {
"required": true,
"path": "customerID"
}
}
}'
以上文件是说我们对于student这个索引类型,定义了它的mapping。重点是将NAME这个字段映射为两个,一个是需要做索引分析的NAME,另一个是不分析的raw,即不会拆分new york这种词组。这样我们在做搜索的时候,就可以对NAME.raw这个字段做term aggregation,获得所有城市出现的次数了。
java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW jackson 2.4.0
java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/filter/TokenFilter jackson 2.6.2
java代码:
package com.jikexueyuancrm.test; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.concurrent.ExecutionException; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.Requests; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; import org.junit.Before; import org.junit.Test; import com.jikexueyuancrm.entity.User; public class ElasticSearch2_3_1 { private Client client; //初始化客户端 @Before public void init() { try { client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); } catch (UnknownHostException e) { e.printStackTrace(); } } @Test // 创建索引 public void createIndex() { client.admin().indices().prepareCreate("test").execute() .actionGet(); } @Test // 定义索引的映射类型 public void defineIndexTypeMapping() { try { XContentBuilder mapBuilder = XContentFactory.jsonBuilder(); mapBuilder .startObject() .startObject("user")//对应于type .startObject("properties") .startObject("id").field("type", "long").field("store", "yes").endObject() .startObject("name").field("type", "string").field("store", "yes").endObject() .startObject("age").field("type", "string").field("index", "not_analyzed").field("store", "yes").endObject() .endObject() .endObject() .endObject(); client.admin().indices().preparePutMapping("test").setType("user").setSource(mapBuilder).execute().actionGet(); } catch (IOException e) { e.printStackTrace(); } } @Test //单个文档创建索引 public void createIndexSingleDocument() { try { IndexResponse response = client .prepareIndex("test", "user", "30") .setSource(// 这里可以直接用json字符串 XContentFactory.jsonBuilder().startObject() .field("id", 1). field("name", "yuanhai") .field("age", "30").endObject()).setTTL(8000).get(); System.out.println("index:" + response.getIndex() + " insert doc id:" + response.getId() + " result:" + response.isCreated()); } catch (IOException e) { e.printStackTrace(); } } @Test //更新修改文档,直接修改 public void updateIndex(){ try { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("test"); updateRequest.type("user"); updateRequest.id("30"); updateRequest.doc(XContentFactory.jsonBuilder() .startObject() .field("age", "100000") .endObject()); UpdateResponse updateResponse = client.update(updateRequest).get(); } catch (Exception e) { e.printStackTrace(); } } @Test //如果文档不存在则插入,存在则修改。 public void updateIndexOrInsert(){ try { IndexRequest indexRequest = new IndexRequest("test", "user", "30") .source(XContentFactory.jsonBuilder() .startObject() .field("id", 1). field("name", "yuanhai") .field("age", "30") .endObject()); UpdateRequest updateRequest = new UpdateRequest("test", "user", "500")//id以这个作为标准 .doc(XContentFactory.jsonBuilder() .startObject() .field("age", "120") .endObject()) .upsert(indexRequest); UpdateResponse updateResponse = client.update(updateRequest).get(); System.out.println(updateResponse.isCreated()); } catch (Exception e) { e.printStackTrace(); } } @Test // 根据id=100删除某个文档 public void deleteIndexById(){ DeleteResponse dResponse=client.prepareDelete("test","user","100").get(); System.out.println(dResponse.getId()); } @Test //删除所有索引,prepareDelete("test")传入可变参数,删除多个索引 public void deleteAllIndex(){ DeleteIndexResponse dResponse= client.admin().indices().prepareDelete("test").get(); System.out.println(dResponse.isAcknowledged()); } //根据ID获取数据 @Test public void getDocById(){ GetResponse response= client.prepareGet("test", "user", "400").get(); System.out.println(response.getSourceAsString()); System.out.println( response.getVersion()); System.out.println( response.getIndex()); System.out.println( response.getType()); System.out.println( response.getId()); } //api跟lucence类似 //在2.x版本中,官方删除了FilterBuilder的所有查询条件,QueryBuilder代替 @Test public void queryIndex(){ SearchRequestBuilder builder = client.prepareSearch("test").setTypes("user").setSearchType(SearchType.DEFAULT).setFrom(0).setSize(100); BoolQueryBuilder qb = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("yuanhai").field("name")) .should(new QueryStringQueryBuilder("太多").field("name")); builder.setQuery(qb); SearchResponse response = builder.execute().actionGet(); System.out.println(response); System.out.println(response.getHits().getTotalHits()); } @Test // 聚合查询类似于facet(facet已废弃) public void Aggregation() { SearchResponse response = client.prepareSearch("test").setTypes("user") .addAggregation(AggregationBuilders.terms("terms") .field("age")).execute().actionGet(); Terms terms = response.getAggregations().get("terms"); for(Bucket b:terms.getBuckets()){ System.out.println("filedname:"+b.getKey()+" docCount:"+b.getDocCount()); } } }