ElasticSearch(五):Java操作ElasticSearch执行查询
package com.gxy.ESChap01; import java.net.InetAddress; 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.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class ESQuery {
private static String host="192.168.56.3"; // 服务器地址
private static int port=9300; // 端口 public static final String CLUSTER_NAME = "my-application"; //集群名称 private TransportClient client=null; private static Settings settings= Settings.builder()
.put("cluster.name",CLUSTER_NAME)
.put("client.transport.sniff", true)
.build(); //获取客户端
@SuppressWarnings({ "resource", "unchecked" })
@Before
public void getClient() throws Exception {
try {
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //关闭客户端
@After
public void close() {
if(client!=null) {
client.close();
}
} /**
* 查询所有
*/
@Test
public void searchAll() {
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
SearchResponse sr=srb.setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();//查询所有
SearchHits hits=sr.getHits();
for(SearchHit hit:hits) {
System.out.println(hit.getSourceAsString());
}
}
/**
* 分页查询
*/
@Test
public void searchPaging() {
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
SearchResponse sr=srb.setQuery(QueryBuilders.matchAllQuery()).setFrom(0).setSize(2).execute().actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
/**
* 排序查询
*/
@Test
public void searchOrderBy() {
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
SearchResponse sr=srb.setQuery(QueryBuilders.matchAllQuery())
.addSort("publishDate",SortOrder.DESC).execute().actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
/**
* 数据列过滤查询
*/
@Test
public void searchInclude() {
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
SearchResponse sr=srb.setQuery(QueryBuilders.matchAllQuery())
.setFetchSource(new String[] {"title","price"},null)
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
/***
* 简单条件查询
*/
@Test
public void searchByCondition() {
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
SearchResponse sr=srb.setQuery(QueryBuilders.matchQuery("title","铁"))
.setFetchSource(new String[] {"title","price"},null)
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
/**
* 条件查询高亮实现
*/
@Test
public void searchHighlight() {
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.preTags("<h2>");
highlightBuilder.postTags("</h2>");
highlightBuilder.field("title");
SearchResponse sr=srb.setQuery(QueryBuilders.matchQuery("title","战"))
.highlighter(highlightBuilder)
.setFetchSource(new String[] {"title","price"},null)
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
System.out.println(hit.getHighlightFields());
}
}
/**
* 多条件查询 must
*/
@Test
public void searchMutil() {
SearchRequestBuilder srb =client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "星球");
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.must(queryBuilder2))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
} /**
* 多条件查询 mustNot
*/
@Test
public void searchMutil2() {
SearchRequestBuilder srb =client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "武士");
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.mustNot(queryBuilder2))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
} /**
* 多条件查询 should提高得分
* @throws Exception
*/
@Test
public void searchMutil3()throws Exception{
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "星球");
QueryBuilder queryBuilder3=QueryBuilders.rangeQuery("publishDate").gt("2018-01-01");
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.should(queryBuilder2)
.should(queryBuilder3))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getScore()+":"+hit.getSourceAsString());
}
}
/***
* 多条件查询 range限制范围
*/
@Test
public void searchMutil4() {
SearchRequestBuilder srb = client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.rangeQuery("price").lte(40);
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.filter(queryBuilder2))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
}
ElasticSearch(五):Java操作ElasticSearch执行查询的更多相关文章
- java操作elasticsearch实现组合桶聚合
1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...
- java操作elasticsearch实现query String
1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...
- java操作elasticsearch实现聚合查询
1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)
1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...
- java操作elasticsearch实现查询删除和查询所有
后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...
- java操作elasticsearch实现批量添加数据(bulk)
java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...
- java操作elasticsearch实现基本的增删改查操作
一.在进行java操作elasticsearch之前,请确认好集群的名称及对应的ES节点ip和端口 1.查看ES的集群名称 #进入elasticsearch.yml配置文件/opt/elasticse ...
- Java操作ElasticSearch之创建客户端连接
Java操作ElasticSearch之创建客户端连接 3 发布时间:『 2017-09-11 17:02』 博客类别:elasticsearch 阅读(3157) Java操作ElasticSe ...
- 使用Java操作Elasticsearch(Elasticsearch的java api使用)
1.Elasticsearch是基于Lucene开发的一个分布式全文检索框架,向Elasticsearch中存储和从Elasticsearch中查询,格式是json. 索引index,相当于数据库中的 ...
随机推荐
- C# Winform开发以及控件开发的需要注意的,被人问怕了,都是基础常识
我是搞控件开发的,经常被人问,所以把一些问题记录了下来!如果有人再问,直接把地址丢给他看. 一. 经常会有人抱怨Winform界面闪烁,下面有几个方法可以尽可能的避免出现闪烁 1.控件的使用尽量以纯色 ...
- 2. 常见的Queue
package com.gf.conn013; import java.util.ArrayList; import java.util.Iterator; import java.util.List ...
- JavaScript中判断整字类型最简洁的实现方法
这篇文章主要介绍了JavaScript中判断整字类型最简洁的实现方法,本文给出多个判断整数的方法,最后总结出一个最短.最简洁的实现方法,需要的朋友可以参考下 我们知道JavaScript提供了type ...
- Html5、css、JavaScript基础
一.HTML学习 HTML样式Css 内联样式- 在HTML元素中使用"style" 属性 内部样式表 -在HTML文档头部 <head> 区域使用<style& ...
- call,apply,bind的用法与区别
1.call/apply/bind方法的来源 首先,在使用call,apply,bind方法时,我们有必要知道这三个方法究竟是来自哪里?为什么可以使用的到这三个方法? call,apply,bind这 ...
- angular ng-repeat 动态获取的dom片段 显示
.filter('to_trusted', ['$sce',function ($sce) { return function (text) { return $sce.trustAsHtml(tex ...
- CSS效果:这里有你想要的CSS3漂亮的自定义Checkbox各种复选框
在原来有一篇文章写到了<CSS效果篇--纯CSS+HTML实现checkbox的思路与实例>. 今天这篇文章主要写各种自定义的checkbox复选框,实现如图所示的复选框: 大致的html ...
- Vue2+VueRouter2+webpack 构建项目实战(五):配置子路由
前言 通过前面几章的实战,我们已经顺利的构建项目,并且从API接口获取到数据并且渲染出来了.制作更多的页面,更复杂的应用,就是各位自己根据自己的项目去调整的事情了. 本章讲一下如何配置子路由,因为我们 ...
- 洛谷P3038 [USACO11DEC]牧草种植Grass Planting
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- Linux 解决Linux下火狐浏览器中文乱码成方块显示问题
解决Linux下火狐浏览器中文乱码成方块显示问题 by:授客 QQ:1033553122 测试环境: CentOS-6.0-x86_64 问题描述: 浏览器页面显示如下 解决方法: 安装中文支 ...