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();
        }
    }
  • 相关阅读:
    Linux shell(3)
    Linux shell 编写(2)
    Linux shell 编写(1)
    团队冲刺(一)
    峦码团队任务表
    电梯演讲&界面展示说明
    第一次小组会议——NABCD讨论
    开发项目&团队介绍
    Linux中查看各文件夹大小命令:du -h --max-depth=1
    shell脚本[] [[]] -n -z 的含义解析
  • 原文地址:https://www.cnblogs.com/dongma/p/13611646.html
Copyright © 2011-2022 走看看