• java 查es


    依赖

     <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>transport</artifactId>
                <version>6.2.4</version>
            </dependency>

    代码

    import org.elasticsearch.action.get.GetResponse;
    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.MatchAllQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.index.query.TermQueryBuilder;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class EsClient {
        public static void main(String[] args) throws UnknownHostException {
            //指定集群
            Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
            // 创建访问es的客户端
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
            // 数据查询
    //        GetResponse response = client.prepareGet("lib3", "user", "1").execute().actionGet();
            // 查询所有
    //        MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
    
            // term 查询
            TermQueryBuilder query = QueryBuilders.termQuery("interests", "喜欢喝酒");
            SearchResponse response = client.prepareSearch("lib3")
                    .setTypes("user")
                    .setQuery(query)
    //                .setSize(3)
                    .get();
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            client.close();
        }
    }
  • 相关阅读:
    UML 基础知识
    制作嵌入式根文件系统
    oracle 学习之 PL/SQL
    Password for '(null)' GNOME keyring:
    oracle 学习笔记
    lua关于编译后无法使用
    android,wince,windows,ios mms 网络电台收音机,小巧,兼容性好,性能高
    lua批量编译目前支持5.2,5.1
    lua关于编译后无法使用
    dlna support windows
  • 原文地址:https://www.cnblogs.com/dongma/p/13611646.html
Copyright © 2020-2023  润新知