ElasticSearch 例子

ElasticSearch是一個接近實時的搜索平臺,它利用Lucese進行文檔索引。java

本文會寫個能夠運行的簡單例子,方便你們上手,往後深刻了解。apache

 

須要引入maven依賴app

<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>

而後是測試主程序elasticsearch

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.propertiesmaven

appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout

rootLogger.level = info
rootLogger.appenderRef.console.ref = console

 

以上就能夠了。測試

tips: jdk8如下可能會遇到問題。ui

相關文章
相關標籤/搜索