• 欢迎浏览“String me = Creater\忠实的资深Linux玩家;”,请文明浏览,理性发言,有侵犯你的权益请邮件我(creater@vip.qq.com).
  • 把任何的失败都当作一次尝试,不要自卑;把所有的成功都想成是一种幸运,不要自傲。
  •    2年前 (2016-08-18)  Elasticsearch |   抢沙发  13 
    文章评分 0 次,平均分 0.0
    [收起] 文章目录

    一、准备数据

     String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别..."));
        String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-06-19", "学习目标 掌握泛型的产生意义..."));
        String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));
        String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基础", "2016-06-19", "Hibernate框架基础..."));
        String data5 = JsonUtil.model2Json(new Blog(5, "Git基本知识git", "2016-06-19", "Shell是什么..."));
        String data6 = JsonUtil.model2Json(new Blog(6, "C++基本知识", "2016-06-19", "Shell是什么..."));
        String data7 = JsonUtil.model2Json(new Blog(7, "Mysql基本知识", "2016-06-19", "git是什么..."));

    二、查询

    一次查询可分为下面四个步骤:

      • 1.创建连接ElasticSearch服务的client.
        索引在ElasticSearch服务器上,进行索引的查询首先要和服务器创建连接,这是第一步。
    Client client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
      • 2.创建QueryBuilder.
        QueryBuilder可以设置单个字段的查询,也可以设置多个字段的查询.
        e.g.1: 查询title字段中包含hibernate关键字的文档:

        QueryBuilder qb1 = termQuery("title", "hibernate");

        e.g.2: 查询title字段或content字段中包含Git关键字的文档:

    QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
      • 3.执行查询
        通过client设置查询的index、type、query.返回一个SearchResponse对象:
    SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
                .actionGet();
      • 4.处理查询结果
        SearchResponse对象的getHits()方法获取查询结果,返回一个SearchHits的集合,遍历集合获取查询的文档信息:
    SearchHits hits = response.getHits();

    三、java代码实现

    package cn.com.bropen.es;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.Client;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.elasticsearch.index.query.MultiMatchQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    
    import static org.elasticsearch.index.query.QueryBuilders.*;
    
    public class ElasticSearchGet {
    
        public static void main(String[] args) {
            // client startup
            try {
                Client client = TransportClient.builder().build()
                        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    
                QueryBuilder qb1 = termQuery("title", "hibernate");
                QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
    
    
                SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
                        .actionGet();
    
                SearchHits hits = response.getHits();
                if (hits.totalHits() > 0) {
                    for (SearchHit hit : hits) {
                        System.out.println("score:"+hit.getScore()+":\t"+hit.getSource());// .get("title")
                    }
                } else {
                    System.out.println("搜到0条结果");
                }
    
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
    
        }
    
    }

    查询结果:

    log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    score:0.5:  {posttime=2016-06-19, id=1, title=git简介, content=SVN与Git最主要的区别...}
    score:0.17673586:   {posttime=2016-06-19, id=7, title=Mysql基本知识, content=git是什么...}
    score:0.049935166:  {posttime=2016-06-19, id=5, title=Git基本知识git, content=Shell是什么...}

    参考资料:

     

     

    除特别注明外,本站所有文章均为String me = "Creater\忠实的资深Linux玩家";原创,转载请注明出处来自http://unix8.net/home.php/5059.html

    关于

    发表评论

    暂无评论

    切换注册

    登录

    忘记密码 ?

    切换登录

    注册

    扫一扫二维码分享