赞
踩
目录
(建里索引、添加数据等略)
@Test
public void query1() {//查询索引名称为indexsearch、索引类型是mysearch,id为11的索引
GetResponse documentFields = client.prepareGet("indexsearch", "mysearch", "11").get();
String index = documentFields.getIndex();//获取索引名称
String type = documentFields.getType();//获取索引类型
String id = documentFields.getId();//获取索引id
System.out.println(index);
System.out.println(type);
System.out.println(id);
Map<String, Object> source = documentFields.getSource();//获取数据源
for (String s : source.keySet()) {
System.out.println(source.get(s));
}
}
@Test
public void queryAll() {
SearchResponse searchResponse = client
.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(new MatchAllQueryBuilder()) //设置查询方式
.get();
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
System.out.println(sourceAsString);
}
client.close();
}
/**
* 查找年龄18到28的人,包含18和28
*/
@Test
public void rangeQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(new RangeQueryBuilder("age").gt(17).lt(29)) //按年龄查询,大于17小于29
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
@Test
public void termQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(new TermQueryBuilder("say", "熟悉")) //按词条查询
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
}
模糊查询可以自动帮我们纠正写错误的英文单词,最大纠正次数两次。
@Test
public void fuzzyQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(QueryBuilders.fuzzyQuery("say", "helOL").fuzziness(Fuzziness.TWO))
.get();//QueryBuilders类里包含了各种查询的方法,上面几种查询也可以通过它来实现
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
模糊匹配查询有两种匹配符,分别是" * " 以及 " ? ";
用" * "来匹配任何多个字符,包括空字符串;
用" ? "来匹配任意的单个字符。
@Test
public void wildCardQueryTest(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(QueryBuilders.wildcardQuery("say", "hel*"))
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
/**
* 多条件组合查询 boolQuery
* 查询年龄是18到28范围内且性别是男性的,或者id范围在10到13范围内的
*/
@Test
public void boolQueryTest(){
RangeQueryBuilder age = QueryBuilders.rangeQuery("age").gt(17).lt(29);
TermQueryBuilder sex = QueryBuilders.termQuery("sex", "1");
RangeQueryBuilder id = QueryBuilders.rangeQuery("id").gt("9").lt("15");
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(
QueryBuilders.boolQuery().should(id) //should相当于or
.should(QueryBuilders.boolQuery().must(sex).must(age))) //must是必须查询
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
@Test
public void getPageIndex(){
int pageSize = 5;//页面大小
int pageNum = 2;//页数
int startNum = (pageNum-1)*5; //起始页面
SearchResponse searchResponse = client.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(QueryBuilders.matchAllQuery())
.addSort("id",SortOrder.ASC) //按id升序查询
.setFrom(startNum) //设置起始页码
.setSize(pageSize) //页码大小
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString()); //返回json字符串
}
client.close();
}
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮查询。
@Test
public void highLight(){
//设置查询高亮字段
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(QueryBuilders.termQuery("say", "hello"));
//设置我们字段高亮的前缀与后缀
HighlightBuilder highlightBuilder = new HighlightBuilder(); //高亮对象(数组)
highlightBuilder.field("say").preTags("<font style='color:red'>").postTags("</font>");
//通过高亮来进行我们的数据查询
SearchResponse searchResponse = searchRequestBuilder.highlighter(highlightBuilder).get();
SearchHits hits = searchResponse.getHits();
System.out.println("查询出来一共"+ hits.totalHits+"条数据");
for (SearchHit hit : hits) {
//打印没有高亮显示的数据
System.out.println(hit.getSourceAsString());
System.out.println("=========================");
//打印我们经过高亮显示之后的数据
Text[] says = hit.getHighlightFields().get("say").getFragments();
for (Text say : says) {
System.out.println(say);
}
}
client.close();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。