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执行查询的更多相关文章

  1. java操作elasticsearch实现组合桶聚合

    1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...

  2. java操作elasticsearch实现query String

    1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...

  3. java操作elasticsearch实现聚合查询

    1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...

  4. java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询

    1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...

  5. java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)

    1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...

  6. java操作elasticsearch实现查询删除和查询所有

    后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...

  7. java操作elasticsearch实现批量添加数据(bulk)

    java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...

  8. java操作elasticsearch实现基本的增删改查操作

    一.在进行java操作elasticsearch之前,请确认好集群的名称及对应的ES节点ip和端口 1.查看ES的集群名称 #进入elasticsearch.yml配置文件/opt/elasticse ...

  9. Java操作ElasticSearch之创建客户端连接

    Java操作ElasticSearch之创建客户端连接 3 发布时间:『 2017-09-11 17:02』  博客类别:elasticsearch  阅读(3157) Java操作ElasticSe ...

  10. 使用Java操作Elasticsearch(Elasticsearch的java api使用)

    1.Elasticsearch是基于Lucene开发的一个分布式全文检索框架,向Elasticsearch中存储和从Elasticsearch中查询,格式是json. 索引index,相当于数据库中的 ...

随机推荐

  1. [PHP]算法-二进制中1的个数的PHP实现

    二进制中1的个数: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: 1.右移位运算>> 和 与运算& 2.先移位个然后再与1 &运算为1的就是1 ...

  2. mysql中general_log查询日志

    作为mysql的通用查询日志,记录增删改查操作的,都有日志文件记录的. 经上网查资料,得知,通用查询日志--可以关闭 show global variables like '%general%'; 同 ...

  3. C# 从Excel中读取条码

    条形码的应用三------从Excel文件中读取条形码 介绍 上一篇文章,我向大家展示了生成多个条形码并存储到Excel文件中的一个方法.后来我又有了个想法:既然条码插入到excel中了,我可不可以从 ...

  4. 阿里云oss,简单上传

    描述:oss比较方便,省去了自己搭建文件服务器的时间,价格比较便宜,下面是java基于oss的简单上传代码 a.添加maven依赖 <dependency> <groupId> ...

  5. ES6之Spread Operater拷贝对象

    译者按: 对象拷贝和合并使用展开运算符(Spread Operator)很方便! 原文: Master Javascript’s New, Cutting-Edge Object Spread Ope ...

  6. sql片段

    1):定义sql片段 <!-- 定义sql片段 --> <!-- id: sql片段的标识 经验:1:基于单表来定义sql片段,这样的话sql片段的可重用性才高 2:sql片段中不要 ...

  7. 【mysql】2003-Can't connect to MySQL server (10060)

    使用navicat或者其他数据库管理工具连接mysql时出现2003-Can’t connect to MySQL server (10060)错误 可能造成出现的原因: 1.网络不通畅 2.mysq ...

  8. gulp解决跨域的配置文件

    //引入插件 var gulp = require('gulp'); // var Proxy = require('gulp-connect-proxy'); var connect = requi ...

  9. zTree 节点文字过多处理方法

    zTree setting.view.addDiyDom 方法可以实现自定义控件,指定节点显示内容.因此需要自己实现addDiyDom方法. 如果树节点不显示checkbox ,处理方法为: func ...

  10. Loadrunner 脚本开发-利用web_submit_data函数实现POST请求

    脚本开发-利用web_submit_data函数实现POST请求   by:授客 QQ:1033553122 概述 web_link()和web_url()函数都是页面访问型函数,实现HTTP请求中的 ...