elasticsearch java API ------搜索
elasticsearch java API ------搜索
在创建索引时,我们根据IndexResponse,得到了index、type和id,Get一条记录的方法很简单:
GetResponse getResponse = client.prepareGet(index, type, id).execute().actionGet();
分布式搜索Elasticsearch——创建索引一文中提到如何将一个实体转化为Json字符串,我们推荐的方法是使用Jackson,那么,在Get得到Response后,也使用Jackson将Json字符串转化为你的实体:
Person newPerson = mapper.readValue(getResponse.getSourceAsString(), Person.class);
简单的搜索:
SearchResponse response = client.prepareSearch("user") .setTypes("tb_person0", "tb_person1", "tb_person2", "tb_person3", "tb_person4") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.fieldQuery("name", "张三")) // Query .setFilter(FilterBuilders.rangeFilter("age").from(20).to(22)) // Filter .setFrom(0).setSize(60).setExplain(true) .execute() .actionGet(); SearchHits hits = response.getHits(); for (SearchHit hit : hits) { String json = hit.getSourceAsString(); Person newPerson = mapper.readValue(json, Person.class); System.out.println("name\t\t" + newPerson.getName()); System.out.println("sex\t\t" + newPerson.getSex()); System.out.println("age\t\t" + newPerson.getAge()); System.out.println("isStudent\t\t" + newPerson.getIsStudent()); }
client.prepareSearch用来创建一个SearchRequestBuilder,搜索即由SearchRequestBuilder执行。
client.prepareSearch方法有参数为一个或多个index,表现在数据库中,即零个或多个数据库名,你既可以使用:
client.prepareSearch().setIndices("index1","index2","index3","index4");
也可以使用以下方式取代:
client.prepareSearch("index1","index2","index3","index4");
SearchRequestBuilder有很多很实用的方法:
(1) setIndices(String... indices):上文中描述过,参数可为一个或多个字符串,表示要进行检索的index;
(2) setTypes(String... types):参数可为一个或多个字符串,表示要进行检索的type,当参数为0个或者不调用此方法时,表示查询所有的type;
(3) setSearchType(SearchType searchType):执行检索的类别,值为org.elasticsearch.action.search.SearchType的元素,SearchType是一个枚举类型的类,其值如下所示:
元素 | 含义 |
QUERY_THEN_FETCH | 查询是针对所有的块执行的,但返回的是足够的信息,而不是文档内容(Document)。 结果会被排序和分级,基于此,只有相关的块的文档对象会被返回。由于被取到的仅仅是这些, 故而返回的hit的大小正好等于指定的size。这对于有许多块的index来说是很便利的 (返回结果不会有重复的,因为块被分组了)。 |
QUERY_AND_FETCH | 最原始(也可能是最快的)实现就是简单的在所有相关的shard上执行检索并返回结果 。每个shard返回一定尺寸的结果。由于每个shard已经返回了一定尺寸的hit, 这种类型实际上是返回多个shard的一定尺寸的结果给调用者。 |
DFS_QUERY_THEN_FETCH | 与QUERY_THEN_FETCH相同 ,预期一个初始的散射相伴用来为更准确的score计算分配了的term频率。 |
DFS_QUERY_AND_FETCH | 与QUERY_AND_FETCH相同 ,预期一个初始的散射相伴用来为更准确的score计算分配了的term频率。 |
SCAN | 在执行了没有进行任何排序的检索时执行浏览 。此时将会自动的开始滚动结果集。 |
COUNT | 只计算结果的数量,也会执行facet。 |
(4) setSearchType(String searchType),与setSearchType(SearchType searchType)类似,区别在于其值为字符串型的SearchType,值可为dfs_query_then_fetch、dfsQueryThenFetch、dfs_query_and_fetch、dfsQueryAndFetch、query_then_fetch、queryThenFetch、query_and_fetch或queryAndFetch;
(5) setScroll(Scroll scroll)、setScroll(TimeValue keepAlive)和setScroll(String keepAlive),设置滚动,参数为Scroll时,直接用new Scroll(TimeValue)构造一个Scroll,为TimeValue或String时需要将TimeValue和String转化为Scroll;
(6) setTimeout(TimeValue timeout)和setTimeout(String timeout),设置搜索的超时时间;
(7) setQuery,设置查询使用的Query;
(8) setFilter,设置过滤器;
(9) setMinScore,设置Score的最小数量;
(10) setFrom,从哪一个Score开始查;
(11) setSize,需要查询出多少条结果;
除以上列举的这些,SearchRequestBuilder还有很多方法,这些方法用于不同的场景,需要大家在实际使用中去体验。
检索出结果后,通过response..getHits()可以得到所有的SearchHit,得到Hit后,便可迭代Hit取到对应的Document,转化成为需要的实体。
前面提到如何进行搜索,并将SearchRequestBuilder的一些方法进行了列举,本文调用了SearchRequestBuilder的用于高亮的方法,处理了检索中的高亮问题:
SearchResponse response1 = client.prepareSearch("user") .setTypes("tb_person0", "tb_person1", "tb_person2", "tb_person3", "tb_person4") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.fieldQuery("name", "张三")) // Query .addHighlightedField("name") .setHighlighterPreTags("<span style=\"color:red\">") .setHighlighterPostTags("</span>") .setFilter(FilterBuilders.rangeFilter("age").from(20).to(22)) // Filter .setFrom(0).setSize(60).setExplain(true) .execute() .actionGet(); SearchHits hits1 = response1.getHits(); for(SearchHit hit : hits1){ String json = hit.getSourceAsString(); Person newPerson = mapper.readValue(json, Person.class); Map<String, HighlightField> result = hit.highlightFields(); HighlightField titleField = result.get("name"); Text[] titleTexts = titleField.fragments(); String name = ""; for(Text text : titleTexts){ name += text; } newPerson.setName(name); System.out.println("name\t\t" + newPerson.getName()); System.out.println("sex\t\t" + newPerson.getSex()); System.out.println("age\t\t" + newPerson.getAge()); System.out.println("isStudent\t\t" + newPerson.getIsStudent()); System.out.println("--------------------------"); }
addHighlightedField(String fieldName)指明要进行高亮处理的Field;setHighlighterPreTags设定了高亮文字的前缀;setHighlighterPostTags设定了高亮文字的后缀。
取得hit后,使用hit.highlightFields()取得结果中进行了高亮标识的域名-域值对,然后对这些域名-域值对进行分析得到高亮的域结果。
//游标使用
public void scan(){
//QueryBuilder qb = QueryBuilders.termQuery("description", "descript");
SearchResponse scrollResp = client.prepareSearch("productindex")
.setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000))
// .setQuery(qb.buildAsBytes())
.setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
//Scroll until no hits are returned
while (true) {
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
boolean hitsRead = false;
for (SearchHit hit : scrollResp.getHits()) {
hitsRead = true;
System.out.println(hit.getSourceAsString());
//Handle the hit...
}
//Break condition: No hits are returned
if (!hitsRead) {
break;
}
}
}
public void query3(){
QueryBuilder qb3 = QueryBuilders.filteredQuery(
QueryBuilders.termQuery("name.first", "shay"),
FilterBuilders.rangeFilter("age")
.from(23)
.to(54)
.includeLower(true)
.includeUpper(false)
);
SearchResponse searchResponse = client.prepareSearch("productindex")
.setQuery(qb3)
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
SearchHits hits = searchResponse.hits();
System.out.println(hits.totalHits());
for (int i = 0; i < hits.totalHits(); i++) {
System.out.println(hits.getAt(i).getSource().toString());
}
}
public void query2(){
QueryBuilder qb2 = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("content", "test1"))
.must(QueryBuilders.termQuery("content", "test4"))
.mustNot(QueryBuilders.termQuery("content", "test2"))
.should(QueryBuilders.termQuery("content", "test3"));
SearchResponse searchResponse = client.prepareSearch("productindex")
.setQuery(qb2)
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
SearchHits hits = searchResponse.hits();
System.out.println(hits.totalHits());
for (int i = 0; i < hits.totalHits(); i++) {
System.out.println(hits.getAt(i).getSource().toString());
}
}
public void query1(){
QueryBuilder qb1 = QueryBuilders.rangeQuery("type").from(0).to(10).includeLower(true).includeUpper(true);
SearchResponse searchResponse = client.prepareSearch("productindex")
.setQuery(qb1)
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
SearchHits hits = searchResponse.hits();
System.out.println(hits.totalHits());
for (int i = 0; i < hits.totalHits(); i++) {
System.out.println(hits.getAt(i).getSource().toString());
}
}
public void query(){
QueryBuilder qb1 = QueryBuilders.termQuery("title", "this");
SearchResponse searchResponse = client.prepareSearch("productindex")
.setQuery(qb1)
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
SearchHits hits = searchResponse.hits();
System.out.println(hits.totalHits());
for (int i = 0; i < hits.totalHits(); i++) {
System.out.println(hits.getAt(i).getSource().get("description"));
}
}
相关推荐
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。