虽然上一篇中的对索引的搜索可以在一定程度上获取索引的信息,但是毕竟功能是有限的,本篇主要是对elasticsearch使用javaAPI实现搜索功能的笔记。
package test;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.index.Terms;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequest;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.SearchTemplateRequestBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
@SuppressWarnings({ "resource","deprecation" })
public class testSearch {
public static void main(String[] args) throws Exception{
searchmethod6();
}
/**
* 方法一
* @throws Exception
*/
public static void searchmethod1() throws Exception{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
SearchResponse response = client.prepareSearch("movies").setTypes("movie").get();
println(response);
for (SearchHit searchHit: response.getHits()) {
println(searchHit);
}
}
/**
* 方法二
* @throws Exception
*/
public static void searchmethod2() throws Exception{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
QueryBuilder qb1 = termQuery("user","10");
// QueryBuilder qb2 = QueryBuilders.multiMatchQuery("git", "title", "content");
SearchResponse response = client.prepareSearch("movies").setQuery(qb1).get();
for (SearchHit searchHit: response.getHits()) {
println(searchHit);
}
}
/**
* 方法三
* @throws Exception
* 这个相当于之前的分页,使用的是Scroll方法
*/
public static void searchmethod3() throws Exception{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
QueryBuilder qb = termQuery("user", "kimchy");
SearchResponse scrollResp = client.prepareSearch("movies")
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(1).get();
do {
for (SearchHit hit : scrollResp.getHits().getHits()) {
println(hit);
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
} while(scrollResp.getHits().getHits().length != 0);
}
/**
* 方法四
* @throws Exception
*/
public static void searchmethod4() throws Exception{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.queryStringQuery("kimchy"));
SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("user", "kimchy"));
MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).get();
for (MultiSearchResponse.Item item : sr.getResponses()) {
SearchResponse response = item.getResponse();
for (SearchHit searchHit : response.getHits()) {
println(searchHit);
}
}
}
/**
* 方法五
* 这个方法先欠着,不是很懂
* @throws Exception
*/
public static void searchmethod5() throws Exception{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
SearchResponse sr = client.prepareSearch()
.setQuery(QueryBuilders.matchAllQuery())
.addAggregation(
AggregationBuilders.terms("agg1").field("field")
)
.addAggregation(
AggregationBuilders.dateHistogram("agg2")
.field("birth")
.dateHistogramInterval(DateHistogramInterval.YEAR)
)
.get();
// Get your facet results
Terms agg1 = sr.getAggregations().get("agg1");
// DateHistogram agg2 = sr.getAggregations().get("agg2");
}
/**
* 方法六
* 能运行,但是就是不知道为什么查询不到结果,同时感觉这种方法很鸡肋,感觉写起来很麻烦,顺便说一下这个东西好像也可以在script下配置,我测试失败,但是感觉没什么用,就不深究了。
* https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-template.html
* @throws Exception
*/
public static void searchmethod6() throws Exception{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
Map json = new HashMap<>();
json.put("param_gender", "1962");
SearchResponse response = new SearchTemplateRequestBuilder(client)
.setScript("{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"year\" : \"{{param_gender}}\"\n" +
" }\n" +
" }\n" +
"}")
.setScriptType(ScriptType.INLINE)
.setScriptParams(json)
.setRequest(new SearchRequest())
.get()
.getResponse();
println(response);
System.out.println(response.getHits().getTotalHits());
for (SearchHit searchHit : response.getHits()) {
println(searchHit);
}
}
/**
* 输出结果SearchResponse
* @param response
*/
public static void println(SearchResponse response){
System.err.println("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
System.err.println(
"getFailedShards : " + response.getFailedShards() + "\n" +
"getNumReducePhases : " + response.getNumReducePhases() + "\n" +
"getScrollId : " + response.getScrollId() + "\n" +
"getTookInMillis : " + response.getTookInMillis() + "\n" +
"getTotalShards : " + response.getTotalShards() + "\n" +
"getAggregations : " + response.getAggregations() + "\n" +
"getProfileResults : " + response.getProfileResults() + "\n" +
"getShardFailures : " + response.getShardFailures() + "\n" +
"getSuggest : " + response.getSuggest() + "\n" +
"getTook : " + response.getTook() + "\n" +
"isTerminatedEarly : " + response.isTerminatedEarly() + "\n" +
"isTimedOut : " + response.isTimedOut() + "\n" +
"remoteAddress : " + response.remoteAddress() + "\n" +
"status : " + response.status() + "\n" +
"getHits : " + response.getHits()
);
}
/**
* 输出结果SearchResponse
* @param response
*/
public static void println(SearchHit searchHit){
System.err.println("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
System.err.println(
"docId : " + searchHit.docId() + "\n" +
"getId : " + searchHit.getId() + "\n" +
"getIndex : " + searchHit.getIndex()+ "\n" +
"getScore : " + searchHit.getScore() + "\n" +
"getSourceAsString : " + searchHit.getSourceAsString() + "\n" +
"getType : " + searchHit.getType() + "\n" +
"getVersion : " + searchHit.getVersion() + "\n" +
"fieldsOrNull : " + searchHit.fieldsOrNull() + "\n" +
"getExplanation : " + searchHit.getExplanation() + "\n" +
"getFields : " + searchHit.getFields() + "\n" +
"highlightFields : " + searchHit.highlightFields() + "\n" +
"hasSource : " + searchHit.hasSource()
);
}
}
DSL就是实现类似schema发出的请求一样的,这方面的介绍主要是在https://www.elastic.co/guide/en/elasticsearch/reference/5.5/getting-started.html和http://www.yiibai.com/elasticsearch/elasticsearch_index_apis.html这两个地方,但是由于这里面的API是真的多,这里就介绍一部分,还有很多东西,可以有需要的时候去官网https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-query-dsl.html看看。
package test;
import static org.elasticsearch.index.query.QueryBuilders.*;
import java.net.InetAddress;
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.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
@SuppressWarnings({"resource","deprecation"})
public class testDSL {
static TransportClient client;
//https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-query-dsl.html
public static void main(String[] args) throws Exception {
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
// _matchAllQuery();
/**
* 从这往下是全文检索的
*/
// _matchQuery();
// _multiMatchQuery();
// _commonTermsQuery();
// _queryStringQuery();
// _simpleQueryStringQuery();
/**
* 从这往下主要运营与数字、日期、枚举等类型的检索
*/
// _termQuery();
// _termsQuery();
// _rangeQuery();
// _existsQuery();
// _prefixQuery();
// _wildcardQuery();
// _fuzzyQuery();
// _typeQuery();
_idsQuery();
}
/**
* 方法一:获取所有
* @throws Exception
*/
public static void _matchAllQuery() throws Exception{
QueryBuilder qb = matchAllQuery();
dealQueryBuilder(qb);
}
/**
* 方法二:指定单查询条件
* @throws Exception
*/
public static void _matchQuery() throws Exception{
QueryBuilder qb = matchQuery(
"user" ,
"kimchy"
);
dealQueryBuilder(qb);
}
/**
* 方法三:指定多查询条件
* @throws Exception
*/
public static void _multiMatchQuery() throws Exception{
QueryBuilder qb = multiMatchQuery(
"kimchy elasticsearch",
"user", "message"
);
dealQueryBuilder(qb);
}
/**
* 方法四:感觉和方法二_matchQuery一样,不知道差别
* @throws Exception
*/
public static void _commonTermsQuery() throws Exception{
QueryBuilder qb = commonTermsQuery("user",
"kimchy");
dealQueryBuilder(qb);
}
/**
* 方法五:+包含 -除外,但是这个查询没有结果,很奇怪
* @throws Exception
*/
public static void _queryStringQuery() throws Exception{
QueryBuilder qb = queryStringQuery("+kimchy -elasticsearch");
dealQueryBuilder(qb);
}
/**
* 方法六:+包含 -除外
* @throws Exception
*/
public static void _simpleQueryStringQuery() throws Exception{
QueryBuilder qb = simpleQueryStringQuery("+kimchy -elasticsearch");
dealQueryBuilder(qb);
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
/**
* 方法七_:termQuery
* @throws Exception
*/
public static void _termQuery() throws Exception{
QueryBuilder qb = termQuery(
"user",
"kimchy"
);
dealQueryBuilder(qb);
}
/**
* 方法八:_termsQuery
* @throws Exception
*/
public static void _termsQuery() throws Exception{
QueryBuilder qb = termsQuery("tags",
"blue", "pill");
dealQueryBuilder(qb);
}
/**
* 方法九:_rangeQuery
* @throws Exception
*/
public static void _rangeQuery() throws Exception{
QueryBuilder qb = rangeQuery("price")
.from(5)
.to(10)
.includeLower(true)
.includeUpper(false);
// A simplified form using gte, gt, lt or lte
QueryBuilder _qb = rangeQuery("age")
.gte("10")
.lt("20");
dealQueryBuilder(qb);
}
/**
* 方法十:_existsQuery
* 匹配含有user字段的记录
* @throws Exception
*/
public static void _existsQuery() throws Exception{
QueryBuilder qb = existsQuery("user");
dealQueryBuilder(qb);
}
/**
* 方法十一:_prefixQuery
* 匹配user中前缀为fds的记录
* @throws Exception
*/
public static void _prefixQuery() throws Exception{
QueryBuilder qb = prefixQuery(
"user",
"fds"
);
dealQueryBuilder(qb);
}
/**
* 方法十二:_wildcardQuery
* 通配符
* @throws Exception
*/
public static void _wildcardQuery() throws Exception{
QueryBuilder qb = wildcardQuery("user", "k?mc*");
dealQueryBuilder(qb);
}
/**
* 方法十三:_fuzzyQuery
* @throws Exception
*/
public static void _fuzzyQuery() throws Exception{
QueryBuilder qb = fuzzyQuery(
"user",
"kimzhy"
);
dealQueryBuilder(qb);
}
/**
* 方法十四:_typeQuery
* @throws Exception
*/
public static void _typeQuery() throws Exception{
QueryBuilder qb = typeQuery("movie");
dealQueryBuilder(qb);
}
/**
* 方法十五:_idsQuery
* 类型是可选的
* @throws Exception
*/
public static void _idsQuery() throws Exception{
QueryBuilder qb = idsQuery("movie")
.addIds("1", "4", "100");
dealQueryBuilder(qb);
}
/**
* 这部分代码的复用性太高了,抽离出来。
* @param qb
*/
public static void dealQueryBuilder(QueryBuilder qb){
SearchResponse response = client.prepareSearch("movies").setQuery(qb).get();
for (SearchHit searchHit : response.getHits()) {
println(searchHit);
}
}
/**
* 输出结果SearchResponse
* @param response
*/
public static void println(SearchHit searchHit){
System.err.println("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
System.err.println(
"docId : " + searchHit.docId() + "\n" +
"getId : " + searchHit.getId() + "\n" +
"getIndex : " + searchHit.getIndex()+ "\n" +
"getScore : " + searchHit.getScore() + "\n" +
"getSourceAsString : " + searchHit.getSourceAsString() + "\n" +
"getType : " + searchHit.getType() + "\n" +
"getVersion : " + searchHit.getVersion() + "\n" +
"fieldsOrNull : " + searchHit.fieldsOrNull() + "\n" +
"getExplanation : " + searchHit.getExplanation() + "\n" +
"getFields : " + searchHit.getFields() + "\n" +
"highlightFields : " + searchHit.highlightFields() + "\n" +
"hasSource : " + searchHit.hasSource()
);
}
}
package test;
import java.net.InetAddress;
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.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
@SuppressWarnings({ "resource", "deprecation" })
public class testUtil {
public static void main(String[] args) throws Exception {
// fenye();
sort();
// highlighter();
}
/**
* 分页
* @throws Exception
*/
public static void fenye() throws Exception {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
SearchResponse response = client.prepareSearch("movies")
.setQuery(QueryBuilders.matchAllQuery())
.setFrom(10)
.setSize(20)
.execute().actionGet();
for (SearchHit searchHit : response.getHits()) {
println(searchHit);
}
}
/**
* 排序
* @throws Exception
*/
public static void sort() throws Exception {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
SearchResponse response = client.prepareSearch("movies")
.setQuery(QueryBuilders.matchAllQuery())
.addSort("postDate", SortOrder.ASC)
.execute().actionGet();
for (SearchHit searchHit : response.getHits()) {
println(searchHit);
}
}
/**
* 高亮
* @throws Exception
*/
public static void highlighter() throws Exception{
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("106.14.112.215"), 9300));
QueryBuilder matchQuery = QueryBuilders.matchQuery("user", "kimchy");
HighlightBuilder hiBuilder=new HighlightBuilder();
hiBuilder.preTags(""
);
hiBuilder.postTags("");
hiBuilder.field("user");
// 搜索数据
SearchResponse response = client.prepareSearch("movies")
.setQuery(matchQuery)
.highlighter(hiBuilder)
.execute().actionGet();
for (SearchHit searchHit : response.getHits()) {
println(searchHit);
}
}
/**
* 输出结果SearchResponse
* @param response
*/
public static void println(SearchHit searchHit){
System.err.println("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
System.err.println(
"docId : " + searchHit.docId() + "\n" +
"getId : " + searchHit.getId() + "\n" +
"getIndex : " + searchHit.getIndex()+ "\n" +
"getScore : " + searchHit.getScore() + "\n" +
"getSourceAsString : " + searchHit.getSourceAsString() + "\n" +
"getType : " + searchHit.getType() + "\n" +
"getVersion : " + searchHit.getVersion() + "\n" +
"fieldsOrNull : " + searchHit.fieldsOrNull() + "\n" +
"getExplanation : " + searchHit.getExplanation() + "\n" +
"getFields : " + searchHit.getFields() + "\n" +
"highlightFields : " + searchHit.highlightFields() + "\n" +
"hasSource : " + searchHit.hasSource()
);
}
}
源码我放在http://download.csdn.net/detail/q15150676766/9920331这里了,有需要的可以去下载看看