Elasticsearch JavaApi
官网JavaApi地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html
博客:http://blog.csdn.net/molong1208/article/details/50512149
1.创建索引与数据
把json字符写入索引,索引库名为twitter、类型为tweet,id为1
语法
import static org.elasticsearch.common.xcontent.XContentFactory.*;
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
相关用例
1 public static boolean create(String index, String type, @Nullable String id,String json){ 2 3 //index:索引库名 4 //type:类型 5 //id:文档的id 6 //json:json字符串 7 //response.isCreated():创建是否成功 8 IndexResponse response = client.prepareIndex(index, type, id) 9 // .setSource("{ \"title\": \"Mastering ElasticSearch\"}") 10 .setSource(json) 11 .execute().actionGet(); 12 13 return response.isCreated(); 14 15 }
2.删除索引与数据
索引库名为twitter、类型为tweet,id为1
语法
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();
相关用例
1 public static boolean remove(String index, String type, String id){ 2 3 //index:索引库名 4 //type:类型 5 //id:文档的id 6 //response.isFound():是否删除成功 7 DeleteResponse response = client.prepareDelete(index, type, id).get(); 8 9 return response.isFound(); 10 11 }
3.修改数据
你可以创建一个UpdateRequest并将其发送到客户端:
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index");
updateRequest.type("type");
updateRequest.id("1");
updateRequest.doc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject());
client.update(updateRequest).get();
相关用例
1 public static boolean update(String index, String type, String id,XContentBuilder endObject) 2 throws IOException, InterruptedException, ExecutionException{ 3 4 // XContentBuilder endObject = XContentFactory.jsonBuilder() 5 // .startObject() 6 // .field("name", "jackRose222") 7 // .field("age", 28) 8 // .field("address","上海徐家汇") 9 // .endObject(); 10 11 //index:索引库名 12 //type:类型 13 //endObject:使用JSON格式返回内容生成器 14 15 UpdateRequest updateRequest = new UpdateRequest(); 16 updateRequest.index(index); 17 updateRequest.type(type); 18 updateRequest.id(id); 19 updateRequest.doc(endObject); 20 UpdateResponse updateResponse = client.update(updateRequest).get(); 21 22 return updateResponse.isCreated(); 23 24 }
也可以用prepareUpdate()
方法
client.prepareUpdate("ttl", "doc", "1")
.setDoc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject())
.get();
相关用例
1 public static boolean update2(String index, String type, String id, 2 MapfieldMap) throws IOException, InterruptedException, ExecutionException{ 3 4 //index:索引库名 5 //type:类型 6 //endObject:使用JSON格式返回内容生成器 7 8 //使用JSON格式返回内容生成器 9 XContentBuilder xcontentbuilder = XContentFactory.jsonBuilder(); 10 11 if(fieldMap!=null && fieldMap.size() >0){ 12 xcontentbuilder.startObject(); 13 14 for (Map.Entry map : fieldMap.entrySet()) { 15 if(map != null){ 16 xcontentbuilder.field(map.getKey(),map.getValue()); 17 } 18 } 19 20 xcontentbuilder.endObject(); 21 22 UpdateResponse updateResponse = client.prepareUpdate(index, type, id) 23 .setDoc(xcontentbuilder) 24 .get(); 25 26 return updateResponse.isCreated(); 27 } 28 29 return false; 30 31 }
4.查询
4.1搜索API允许一个执行一个搜索查询,返回搜索结果匹配的查询。它可以跨越一个或多个指标和执行一个或多个类型。查询可以使用查询提供的Java API。搜索请求的主体使用SearchSourceBuilder构建。这是一个例子:
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders.*;
SearchResponse response = client.prepareSearch("index1", "index2")
.setTypes("type1", "type2")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
.setFrom(0).setSize(60).setExplain(true)
.get();
请注意,所有参数都是可选的。这是最小的搜索你可以写
// MatchAll on the whole cluster with all default options
SearchResponse response = client.prepareSearch().get();
尽管Java API定义了额外的搜索类型QUERY_AND_FETCH DFS_QUERY_AND_FETCH,这些模式内部优化和不应该由用户显式地指定的API。
相关用例
1 public static SearchResponse search(String index, String type) { 2 3 // 查询全部 4 // SearchResponse response2 = 5 // client.prepareSearch().execute().actionGet(); 6 7 // 按照索引与类型查询 8 //index:索引库名 9 //type:类型 10 SearchResponse response = client.prepareSearch(index).setTypes(type) 11 // .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) 12 // .setQuery(QueryBuilders.termQuery("multi", "test")) // Query 13 // .setFrom(0) 14 // .setSize(5) 15 // .setExplain(true) 16 .execute().actionGet(); 17 return response; 18 }
4.2多条件查询
http://blog.csdn.net/zx711166/article/details/77847120
1 public class EsBool{ 2 public void BoolSearch(TransportClient client){ 3 //多条件设置 4 MatchPhraseQueryBuilder mpq1 = QueryBuilders 5 .matchPhraseQuery("pointid","W3.UNIT1.10LBG01CP301"); 6 MatchPhraseQueryBuilder mpq2 = QueryBuilders 7 .matchPhraseQuery("inputtime","2016-07-21 00:00:01"); 8 QueryBuilder qb2 = QueryBuilders.boolQuery() 9 .must(mpq1) 10 .must(mpq2); 11 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); 12 sourceBuilder.query(qb2); 13 //System.out.println(sourceBuilder.toString()); 14 15 //查询建立 16 SearchRequestBuilder responsebuilder = client 17 .prepareSearch("pointdata").setTypes("pointdata"); 18 SearchResponse myresponse=responsebuilder 19 .setQuery(qb2) 20 .setFrom(0).setSize(50) 21 .addSort("inputtime", SortOrder.ASC) 22 //.addSort("inputtime", SortOrder.DESC) 23 .setExplain(true).execute().actionGet(); 24 SearchHits hits = myresponse.getHits(); 25 for(int i = 0; i < hits.getHits().length; i++) { 26 System.out.println(hits.getHits()[i].getSourceAsString()); 27 28 } 29 } 30 }
posted @
2017-12-04 19:03 人猿亦猿 阅读(
...) 评论(
...) 编辑 收藏