zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    使用Netty4实现基本的消息分发
    【Netty官方文档翻译】引用计数对象(reference counted objects)
    nio复习总结
    redis tutorail
    服装设计
    linux nat网络配置
    关闭linux退格键和vi发出的嘟嘟声
    CentOS/Linux 网卡设置 IP地址配置
    WCF Security基本概念(转载)
    WCF使用net.tcp寄宿到IIS中(转)
  • 原文地址:https://www.cnblogs.com/dongma/p/13611646.html
Copyright © 2011-2022 走看看