java操作elecsearch
Posted ruijiege
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java操作elecsearch相关的知识,希望对你有一定的参考价值。
操作Java
import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.get.GetRequestBuilder; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; import java.net.InetAddress; import java.util.HashMap; import java.util.List; import java.util.Map; public class sad { public TransportClient getClient() throws Exception{ Settings settings = Settings.builder() .put("client.transport.sniff", true).build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); return client; } //保存数据 @Test public void save() throws Exception { TransportClient client = getClient(); IndexRequestBuilder builder = client.prepareIndex("crm", "employee", "" + 1); Map map = new HashMap<String,String>(); map.put("name","jiedada"); map.put("id",1); map.put("age",18); builder.setSource(map).get(); client.close(); } //查询数据 @Test public void find() throws Exception { TransportClient client = getClient(); GetResponse response = client.prepareGet("crm", "employee", "1").get(); System.out.println(response.getSource()); client.close(); } //批量添加elec @Test public void findBatch() throws Exception { TransportClient client = getClient(); BulkRequestBuilder builder = client.prepareBulk(); Map map = new HashMap(); map.put("id",1); map.put("name","jiedada"); map.put("age",18); IndexRequestBuilder request = client.prepareIndex("crm","employee","1").setSource(map); Map map2 = new HashMap(); map2.put("id",2); map2.put("name","laojie"); map2.put("age",40); IndexRequestBuilder request2 = client.prepareIndex("crm","employee","2").setSource(map2); builder.add(request); builder.add(request2);//该方法ES默认是分片1秒钟后刷新,即插入成功后马上查询,插入的数据不能马上被查出 BulkResponse response = builder.get(); System.out.println(response); if(response.hasFailures()) { System.out.println("操作失败"); } } //条件查询 @Test public void serchBatch() throws Exception { TransportClient client = getClient(); //是往那个索引库中查询数据 SearchRequestBuilder builder = client.prepareSearch("crm") .setTypes("employee"); //查询的范围 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); List<QueryBuilder> must = boolQuery.must(); //query bool must must.add(QueryBuilders.matchQuery("name","jiedada")); //可以添加多个 //filter boolQuery.filter(QueryBuilders.rangeQuery("age").gte(10).lte(30));//query bool filter //query builder.setQuery(boolQuery); //query bool //排序 builder.addSort("age", SortOrder.ASC); //分页 builder.setFrom(0).setSize(10); //展示内容 SearchResponse response = builder.get(); //获取总数 SearchHits hits = response.getHits(); //搜索出来东西都放到这个对象里面,里面包含总数+数据 System.out.println("总数:"+hits.totalHits()); SearchHit[] searchHits = hits.getHits(); for (SearchHit searchHit : searchHits) { System.out.println(searchHit.getSource()); } client.close(); } }
以上是关于java操作elecsearch的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.IllegalStateException:onSaveInstanceState On Activity Backpress 后无法执行此操作(不使用任何片段)
VSCode自定义代码片段15——git命令操作一个完整流程