DFS query then fetch (可以更精确控制索引打分和排名)
在query then fetch的基础之上,将索引关键字拆分成若干个小的关键字,从索引库中计算各个词频,同时触发的score计算,以便找到最贴合的数据信息。
@Test def testQuery(): Unit = {
val response = client.prepareSearch(indices: _*)
.setSearchType(SearchType.DEFAULT) //设置检索类型
//第一个参数:要在哪一个字段进行查询,第二个参数指定是要查询什么内容
/**
* select
* xxx
* from t
* where name like '%text%'
*/
// .setQuery(QueryBuilders.matchQuery("name", "kafka")) //设置检索方式 全文检索的过程中不区分大小写
.setQuery(QueryBuilders.prefixQuery("name", "a")) // name like 'a%'
.get()//执行,并获取检索内容
val hits = response.getHits //取出检索结果 {"hits": {"total": 3, "maxScore": 1.0, "hits": [{"_index":"product","_type":"bigdata","_id":"5","_score":1.0,"_source":{"author":"LinkedIn","name":"kafka","version":"0.10.0.1"}}]}
val total = hits.totalHits //总共检索到的数据条数(long)
val maxScore = hits.getMaxScore//最大得分
val searchHits = hits.getHits //拿到所有的检索结果
//{"_index":"product","_type":"bigdata","_id":"5","_score":1.0,"_source":{"author":"LinkedIn","name":"kafka","version":"0.10.0.1"}}
for(searchHit <- searchHits) {
val index = searchHit.getIndex
val `type` = searchHit.getType
val id = searchHit.getId
val score = searchHit.getScore
val source = searchHit.getSourceAsString
val jsonObj = new JSONObject();
jsonObj.put("_index", index)
jsonObj.put("_type", `type`)
jsonObj.put("_id", id)
jsonObj.put("_score", score)
jsonObj.put("_source", new JSONObject(source))
println(jsonObj.toString)
}
}
2)分页查询
val searchHits = hits.getHits
println("--------------开始输出结果:--------------------")
for(searchHit <- searchHits) {
val source = searchHit.getSourceAsMap
val age = source.get("age")
val balance = source.get("balance")
val gender = source.get("gender")
val firstname = source.get("firstname")
@Test def testQueryHighLight(): Unit = {
val response = client.prepareSearch(indices: _*)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(QueryBuilders.matchQuery("address", "Avenue"))
.highlighter(new HighlightBuilder()
.field("address")//给哪个字段设置高亮
.preTags("<font color='red' size='20px'>")//前置标签
.postTags("</font>") //后置标签
)
.setFrom(0)
.setSize(5)
.get()
val hits = response.getHits
val totalHits = hits.getTotalHits
println("OLD Li为您找到相关结果约" + totalHits + "个")
val searchHits = hits.getHits
println("--------------开始输出结果:--------------------")
for(searchHit <- searchHits) {
val source = searchHit.getSourceAsMap
val age = source.get("age")
val balance = source.get("balance")
val gender = source.get("gender")
val firstname = source.get("firstname")
private var client:TransportClient = null
private[test] val indices = Array("chinese1")
@Before def setUp(): Unit = {
client = ElasticSearchUtil.getTransportClient;
}
/**
* 在索引库chinese中检索包含”中“的文档
*/
@Test def testQuery(): Unit = {
val response = client.prepareSearch(indices: _*)
.setSearchType(SearchType.DEFAULT) //设置检索类型
/*
matchQuery会将检索关键字进行分词,
将"中国"做了分词:
中
国
termQuery在检索过程中,没有对的关键字进行拆分,按照整体进行查询
英文的分词,天然就是按照空格分隔一个个的单词,这些软件默认对中文的分词就是按照一个个汉字来进行拆分
*/
// .setQuery(QueryBuilders.matchQuery("content", "中国")) // name like 'a%'
.setQuery(QueryBuilders.termQuery("content", "中国"))
.get()//执行,并获取检索内容
val hits = response.getHits //取出检索结果 {"hits": {"total": 3, "maxScore": 1.0, "hits": [{"_index":"product","_type":"bigdata","_id":"5","_score":1.0,"_source":{"author":"LinkedIn","name":"kafka","version":"0.10.0.1"}}]}
val total = hits.totalHits //总共检索到的数据条数(long)
println("OLD Li为您找到相关结果约" + total + "个")
val maxScore = hits.getMaxScore//最大得分
val searchHits = hits.getHits //拿到所有的检索结果
for(searchHit <- searchHits) {
val index = searchHit.getIndex
val `type` = searchHit.getType
val id = searchHit.getId
val score = searchHit.getScore
val source = searchHit.getSourceAsString
val jsonObj = new JSONObject();
jsonObj.put("_index", index)
jsonObj.put("_type", `type`)
jsonObj.put("_id", id)
jsonObj.put("_score", score)
jsonObj.put("_source", new JSONObject(source))
println(jsonObj.toString)
}
}
@After def cleanUp(): Unit = {
ElasticSearchUtil.close(client);
}
}