在点击测评前,请在右侧命令行手动启动 Elasticsearch。
su es
/opt/install/elasticsearch-6.5.4/bin/elasticsearch`
第1关:常见基本查询
package com.test;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
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.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ElasticsearchBasicQuery {
public final static String HOST = "127.0.0.1";
public final static int PORT = 9300;
public static void main(String[] args) throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));
/********** Begin **********/
// 构建查询条件
QueryBuilder builder =QueryBuilders.queryStringQuery("+淘宝 +i7 +联想");
// 执行查询
SearchResponse sResponse = client.prepareSearch("blog3") // 替换为你的索引名称
.setQuery(builder)
.get();
// 获取查询结果
SearchHits hits = sResponse.getHits();
// 输出符合条件的文档数据
for (SearchHit hit : hits) {
// 打印内容
System.out.println(hit.getSourceAsString());
}
/********** End **********/
client.close();
}
}
第2关:组合查询(复合查询)
package com.test;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
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.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ElasticsearchSeniorQuery {
public final static String HOST = "127.0.0.1";
public final static int PORT = 9300;
public static void main(String[] args) throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));
/********** Begin **********/
QueryBuilder builder = QueryBuilders.boolQuery()
.must(QueryBuilders
.rangeQuery("book_price")
.gte(30))
.mustNot(QueryBuilders
.matchQuery("book_genre", "长"))
.filter(QueryBuilders
.rangeQuery("release_date")
.gte("1900-01-01")
.format("yyyy-MM-dd"));
SearchResponse sResponse = client.prepareSearch("book")
.setQuery(builder)
.get();
SearchHits hits = sResponse.getHits();
for(SearchHit hit:hits) {
System.out.println(hit.getSourceAsString());
}
/********** End **********/
client.close();
}
}
第3关:聚合查询
package com.test;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ElasticsearchAggQuery {
public final static String HOST = "127.0.0.1";
public final static int PORT = 9300;
public static void main(String[] args) throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));
/********** Begin **********/
QueryBuilder builder = QueryBuilders.boolQuery()
.must(QueryBuilders
.rangeQuery("book_price")
.gte(20))
.mustNot(QueryBuilders
.matchQuery("book_genre", "长"));
AggregationBuilder agg= AggregationBuilders.extendedStats("book_stats").field("book_price");
SearchResponse sResponse = client.prepareSearch("book")
.setQuery(builder)
.addAggregation(agg)
.get();
ExtendedStats book_stats= sResponse.getAggregations().get("book_stats");
double min = book_stats.getMin();
double max = book_stats.getMax();
double avg = book_stats.getAvg();
double sum = book_stats.getSum();
long count = book_stats.getCount();
System.out.println("最小值:"+min);
System.out.println("最大值:"+max);
System.out.println("平均值:"+avg);
System.out.println("总和:"+sum);
System.out.println("数量为:"+count);
/********** End **********/
client.close();
}
}