elasticsearch__2__java操作之数据搜索查询

来源:转载




elasticsearch 分布式搜索系列专栏:http://blog.csdn.net/xiaohulunb/article/category/2399789

内容涉及代码GitHub地址: 点击打开链接




数据搜索、修改数据操作

/** * 搜索,通过Id搜索API * * @param id */ public void searchById(String id) { GetResponse responsere = client.prepareGet(INDEX_DEMO_01, INDEX_DEMO_01_MAPPING, id) /* 设置线程 当删除api在同一个节点上执行时(在一个分片中执行一个api会分配到同一个服务器上), 删除api允许执行前设置线程模式 (operationThreaded选项),operationThreaded这个选项是使这个操作在另外一个线程中执行, 或在一个正在请求的线程 (假设这个api仍是异步的)中执行。 默认的话operationThreaded会设置成true,这意味着这个操作将在一个不同的线程中执行。 下面是 设置成false的方法: */ .setOperationThreaded(false) .execute() .actionGet(); if (responsere.isExists()) { System.out.println("通过Id=[" + id + "]搜索结果:/n" + responsere.getSourceAsString()); } else { System.out.println("通过Id=[" + id + "]搜索结果:不存在"); } } /** * 搜索,Query搜索API * 条件组合查询 */ public void searchByQuery() { //qb1构造了一个TermQuery,对name这个字段进行项搜索,项是最小的索引片段,这个查询对应lucene本身的TermQuery QueryBuilder queryBuilder1 = QueryBuilders.termQuery("name", "葫芦2娃"); //qb2构造了一个组合查询(BoolQuery),其对应lucene本身的BooleanQuery,可以通过must、should、mustNot方法对QueryBuilder进行组合,形成多条件查询 QueryBuilder queryBuilder2 = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("note", "test1")) .must(QueryBuilders.termQuery("note", "test4")) .mustNot(QueryBuilders.termQuery("note", "test2")) .should(QueryBuilders.termQuery("note", "test3")); //直接执行搜索 SearchHit[] searchHitsBySearch = client.search(new SearchRequest(INDEX_DEMO_01) .types(INDEX_DEMO_01_MAPPING) .source( SearchSourceBuilder.searchSource() .sort("age") ) ) .actionGet() .getHits() .hits(); //预准备执行搜索 client.prepareSearch(INDEX_DEMO_01) .setTypes(INDEX_DEMO_01_MAPPING) // .setSearchType(SearchType.SCAN) //.setQuery(queryBuilder1) //.setQuery(QueryBuilders.termQuery("multi", "test")) // Query //.setPostFilter(FilterBuilders.rangeFilter("age").lt(10).gt(50)) // Filter过滤 //.setPostFilter(FilterBuilders.inFilter("age", 45)) // Filter过滤 .setPostFilter(FilterBuilders.boolFilter().mustNot(FilterBuilders.inFilter("age", 20, 21, 22))) .setFrom(0).setSize(60).setExplain(true) .execute() //注册监听事件 .addListener(new ActionListener<SearchResponse>() { @Override public void onResponse(SearchResponse searchResponse) { SearchHit[] searchHitsByPrepareSearch = searchResponse.getHits().hits(); //获取结果集 for (SearchHit searchHit : searchHitsByPrepareSearch) { System.out.println(searchHit.getSourceAsString()); } } @Override public void onFailure(Throwable e) { } }); } /** * 搜索,Query搜索API * count查询 */ public void searchByQuery_Count() { long countByCount = client.count( new CountRequest(INDEX_DEMO_01).types(INDEX_DEMO_01_MAPPING) ) .actionGet() .getCount(); //预准备 long countByPrepareCount = client.prepareCount(INDEX_DEMO_01) .setTypes(INDEX_DEMO_01_MAPPING) .setQuery(QueryBuilders.termQuery("name", "葫芦1娃")) .execute() .actionGet() .getCount(); System.out.println("searchByQuery_Count<{}>:" + countByCount); } /** * 修改 */ public void updateByQuery() throws IOException { boolean isCreatedByUpdate = client.update(new UpdateRequest(INDEX_DEMO_01, INDEX_DEMO_01_MAPPING, "TKLkVot6SJu429zpJbFN3g") .doc(XContentFactory.jsonBuilder() .startObject() .field("name", "liw") .field("age", "25") .endObject() ) ) .actionGet() .isCreated(); //预准备 client.prepareUpdate(INDEX_DEMO_01, INDEX_DEMO_01_MAPPING, "TKLkVot6SJu429zpJbFN3g") .setDoc("age", "18") .execute() .actionGet(); }




分享给朋友:
您可能感兴趣的文章:
随机阅读: