ElasticSearch 例子
Posted 知己一生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearch 例子相关的知识,希望对你有一定的参考价值。
ElasticSearch是一个接近实时的搜索平台,它利用Lucese进行文档索引。
本文会写个可以运行的简单例子,方便大家上手,日后深入了解。
需要引入maven依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
然后是测试主程序
import org.elasticsearch.action.index.IndexResponse; 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.SearchHits; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.transport.client.PreBuiltTransportClient; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * Created by joe on 2017/9/25. */ public class ElasticSearch { public static void main(String[] args) { TransportClient client; Settings esSettings = Settings.builder() .put("cluster.name", "elasticsearch") //设置ES实例的名称 .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中 .build(); client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。 //此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置 try { client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); } catch (UnknownHostException e) { e.printStackTrace(); } Map<String,Object> infoMap = new HashMap<String, Object>(); infoMap.put("name", "广告信息11"); infoMap.put("title", "我的广告22"); infoMap.put("createTime", new Date()); infoMap.put("count", 1022); IndexResponse indexResponse = client.prepareIndex("test", "info","100").setSource(infoMap). execute().actionGet(); QueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("count").gt(50); SearchResponse searchResponse = client.prepareSearch("test") .setTypes("info") .setQuery(rangeQueryBuilder) .addSort("count", SortOrder.DESC) .setSize(20) .execute() .actionGet(); SearchHits hits = searchResponse.getHits(); System.out.println("查到记录数:" + hits.getTotalHits()); SearchHit[] searchHists = hits.getHits(); if(searchHists.length>0){ for(SearchHit hit:searchHists){ String name = (String) hit.getSource().get("name"); Integer age = (Integer)hit.getSource().get("count"); String date = (String) hit.getSource().get("createTime"); System.out.format("name:%s ,count :%d, createTime:%s\n", name, age, date); } } } }
不要忘了配置 log4j2.properties
appender.console.type = Console appender.console.name = console appender.console.layout.type = PatternLayout rootLogger.level = info rootLogger.appenderRef.console.ref = console
以上就可以了。
tips: jdk8以下可能会遇到问题。
以上是关于ElasticSearch 例子的主要内容,如果未能解决你的问题,请参考以下文章