spring 操作elasticsearch查询使用方法

最近学习了一下elasticsearch使用,网上的资料又很少,真是一个头两个大。好歹最后终于了解了。留个笔记做日后查询。

package com.gooddeep.dev.elasticsearch.commons.dao; 

import java.util.ArrayList;
import java.util.List;
import java.util.Map; 

import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder.Field;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.stereotype.Component; 

import com.gooddeep.dev.core.helper.PropertyHelper;
import com.gooddeep.dev.core.helper.UuidHelper;
import com.gooddeep.dev.core.model.BasePage;
import com.gooddeep.dev.elasticsearch.commons.model.EsBaseBean;
import com.gooddeep.dev.elasticsearch.commons.service.EsBaseService; 

@Component("esBaseDao")
public abstract class EsBaseDaoImpl<T> implements EsBaseDao<T> { 

  private Logger logger = LoggerFactory.getLogger(EsBaseService.class); 

  @Autowired
  private ElasticsearchTemplate elasticsearchTemplate; 

  @Autowired
  private Client esClient; 

  /**
   * 插入或等新,需要有id,id需要自己生成
   *
   * @param tList
   * @return
   */
  public boolean insertOrUpdate(List<T> tList) {
    List<IndexQuery> queries = new ArrayList<IndexQuery>();
    for (T t : tList) {
      String id = ((EsBaseBean) t).getId();
      if (id == null) {
        id = UuidHelper.getRandomUUID();
        ((EsBaseBean) t).setId(id);
      }
      IndexQuery indexQuery = new IndexQueryBuilder().withId(id).withObject(t).build();
      queries.add(indexQuery);
    }
    elasticsearchTemplate.bulkIndex(queries);
    return true;
  } 

  /**
   * 插入或更新
   *
   * @param t
   * @return
   */
  public boolean insertOrUpdate(T t) { 

    String id = ((EsBaseBean) t).getId();
    if (id == null) {
      id = UuidHelper.getRandomUUID();
      ((EsBaseBean) t).setId(id);
    }
    try {
      IndexQuery indexQuery = new IndexQueryBuilder().withId(id).withObject(t).build();
      elasticsearchTemplate.index(indexQuery);
      return true;
    } catch (Exception e) {
      logger.error("insert or update user info error.", e);
      return false;
    }
  } 

  /**
   * 删除
   *
   * @param id
   * @return
   */
  public boolean deleteById(String id) {
    try {
      elasticsearchTemplate.delete(getEntityClass(), id);
      return true;
    } catch (Exception e) {
      logger.error("delete " + getEntityClass() + " by id " + id
          + " error.", e);
      return false;
    }
  } 

  /**
   * 删除ids
   * @param idList
   * @return
   */
  @Override
  public boolean deleteByIds(List<String> idList) {
    try {
       CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria());
       criteriaQuery.setIds(idList);
       elasticsearchTemplate.delete(criteriaQuery, getEntityClass());
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  } 

  /**
   * 根据条件查询
   * @param filedContentMap 不能为null
   * @return
   */
  public boolean deleteByQuery(Map<String,Object> filedContentMap) {
    try {
      DeleteQuery dq = new DeleteQuery(); 

      BoolQueryBuilder qb=QueryBuilders. boolQuery();
      if(filedContentMap!=null)
        for (String key : filedContentMap.keySet()) {//字段查询
          qb.must(QueryBuilders.matchQuery(key,filedContentMap.get(key)));
        }
      dq.setQuery(qb);;
      elasticsearchTemplate.delete(dq, getEntityClass());;
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 检查健康状态
   *
   * @return
   */
  public boolean ping() {
    try {
      ActionFuture<ClusterHealthResponse> health = esClient.admin()
          .cluster().health(new ClusterHealthRequest());
      ClusterHealthStatus status = health.actionGet().getStatus();
      if (status.value() == ClusterHealthStatus.RED.value()) {
        throw new RuntimeException(
            "elasticsearch cluster health status is red.");
      }
      return true;
    } catch (Exception e) {
      logger.error("ping elasticsearch error.", e);
      return false;
    }
  } 

  /**
   * 条件查询
   *
   * @param searchfields
   *      查询字段
   * @param filedContentMap
   *      字段和查询内容
   * @param sortField
   *      排序 字段
   * @param order
   *      排序
   * @param from
   * @param size
   * @return
   */
  @Override
  public BasePage<T> queryPage(Map<String,Object> filedContentMap, final List<String> heightFields, String sortField, SortOrder order, BasePage<T>basePage) { 

    Field[] hfields=new Field[0];
    if(heightFields!=null)
    {
      hfields = new Field[heightFields.size()];
      for (int i = 0; i < heightFields.size(); i++) {
        hfields[i] = new HighlightBuilder.Field(heightFields.get(i)).preTags("<em style='color:red'>").postTags("</em>").fragmentSize(250);
      }
    }
    NativeSearchQueryBuilder nsb = new NativeSearchQueryBuilder().withHighlightFields(hfields);//高亮字段
    if (sortField != null && order != null)//排序
      nsb.withSort(new FieldSortBuilder(sortField).ignoreUnmapped(true).order(order));
    if (basePage != null)//分页
      nsb.withPageable(new PageRequest(basePage.getPageNo(), basePage.getPageSize()));
    BoolQueryBuilder qb=QueryBuilders. boolQuery();
    for (String key : filedContentMap.keySet()) {//字段查询
      qb.must(QueryBuilders.matchQuery(key,filedContentMap.get(key))); 

    }
    //userKey=78e48b85e94911e0d285f4eec990d556
    //fa6e9c5bb24a21807c59e5fd3b609e12
    nsb.withQuery(qb);
    SearchQuery searchQuery = nsb.build();//查询建立 

    Page<T> page = null;
    if (heightFields!=null&&heightFields.size() > 0) {//如果设置高亮
      page = elasticsearchTemplate.queryForPage(searchQuery,
          getEntityClass(), new SearchResultMapper() {
            @SuppressWarnings("unchecked")
            @Override
            public <T> Page<T> mapResults(SearchResponse response,Class<T> clazz, Pageable pageable) {
              List<T> chunk = new ArrayList<T>();
              for (SearchHit searchHit : response.getHits()) {
                if (response.getHits().getHits().length <= 0) {
                  return null;
                } 

                Map<String, Object> entityMap = searchHit.getSource();
                for (String highName : heightFields) {
                  Text text[]=searchHit.getHighlightFields().get(highName).fragments();
                  if(text.length>0)
                  {
                    String highValue = searchHit.getHighlightFields().get(highName).fragments()[0].toString();
                    entityMap.put(highName, highValue);
                  }
                }
                chunk.add((T) PropertyHelper.getFansheObj(
                    getEntityClass(), entityMap));
              }
              if (chunk.size() > 0) {
                return new PageImpl<T>((List<T>) chunk);
              }
              return new PageImpl<T>(new ArrayList<T>());
            } 

          });
    } else//如果不设置高亮
    {
      logger.info("#################"+qb.toString());
      page = elasticsearchTemplate.queryForPage(searchQuery,getEntityClass());
    } 

  // List<T> ts = page.getContent(); 

    basePage.setTotalRecord(page.getTotalElements());
    basePage.setResults(page.getContent());
    return basePage;
  } 

  @Override
  public List<T> queryList(Map<String, Object> filedContentMap,final List<String> heightFields, String sortField, SortOrder order) {
    Field[] hfields=new Field[0];
    if(heightFields!=null)
    {
      hfields = new Field[heightFields.size()];
      for (int i = 0; i < heightFields.size(); i++) {
        //String o="{\"abc\" : \"[abc]\"}";
        hfields[i] = new HighlightBuilder.Field(heightFields.get(i)).preTags("<em>").postTags("</em>").fragmentSize(250);
      }
    }
    NativeSearchQueryBuilder nsb = new NativeSearchQueryBuilder().withHighlightFields(hfields);//高亮字段
    if (sortField != null && order != null)//排序
      nsb.withSort(new FieldSortBuilder(sortField).ignoreUnmapped(true).order(order));
    BoolQueryBuilder qb=QueryBuilders. boolQuery();
    for (String key : filedContentMap.keySet()) {//字段查询
      qb.must(QueryBuilders.matchQuery(key,filedContentMap.get(key))); 

    }
    nsb.withQuery(qb);
    SearchQuery searchQuery = nsb.build();//查询建立
    Page<T> page = null;
    if (heightFields!=null&&heightFields.size() > 0) {//如果设置高亮
      page = elasticsearchTemplate.queryForPage(searchQuery,
          getEntityClass(), new SearchResultMapper() {
            @SuppressWarnings("unchecked")
            @Override
            public <T> Page<T> mapResults(SearchResponse response,Class<T> clazz, Pageable pageable) {
              List<T> chunk = new ArrayList<T>();
              for (SearchHit searchHit : response.getHits()) {
                if (response.getHits().getHits().length <= 0) {
                  return null;
                } 

                Map<String, Object> entityMap = searchHit.getSource();
                for (String highName : heightFields) {
                  String highValue = searchHit.getHighlightFields().get(highName).fragments()[0].toString();
                  entityMap.put(highName, highValue);
                }
                chunk.add((T) PropertyHelper.getFansheObj(getEntityClass(), entityMap));
              }
              if (chunk.size() > 0) {
                return new PageImpl<T>((List<T>) chunk);
              }
              return null;
            } 

          });
    } else//如果不设置高亮
      page = elasticsearchTemplate.queryForPage(searchQuery,getEntityClass()); 

    return page.getContent();
  }
  /**
   * 本类查询
   *
   * @param id
   * @return
   */
  public T queryById(String id) {
    StringQuery stringQuery = new StringQuery("id=" + id);
    T t = elasticsearchTemplate.queryForObject(stringQuery,
        getEntityClass());
    return t; 

  } 

  public ElasticsearchTemplate getElasticsearchTemplate() {
    return elasticsearchTemplate;
  } 

  public Client getEsClient() {
    return esClient;
  } 

  /**
   * 得到类型
   *
   * @return
   */
  public abstract Class<T> getEntityClass();
  /**
   * 添加各自类的影射
   */
  public abstract void putClassMapping(); 

} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 安装ElasticSearch搜索工具并配置Python驱动的方法

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便. 我们建立一个网站或应用程序,并要添加搜索功能,令我们受打击的是:搜索工作是很难的.我们希望我们的搜索解决方案要快,我们希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单

  • 详解spring中使用Elasticsearch的代码实现

    在使用Elasticsearch之前,先给大家聊一点干货. 1.      ES和solr都是作为全文搜索引擎出现的.都是基于Lucene的搜索服务器. 2.   ES不是可靠的存储系统,不是数据库,它有丢数据的风险. 3.  ES不是实时系统,数据写入成功只是trans log成功(类似于MySQL的bin log),写入成功后立刻查询查不到是正常的.因为数据此刻可能还在内存里而不是进入存储引擎里.同理,删除一条数据后也不是马上消失.写入何时可查询?ES内部有一个后台线程,定时将内存中的一批数

  • elasticsearch插件开发教程

    检索引擎Elasticsearch支持插件模式.有些时候你可能须要安装一些插件.甚至自己开发插件,这里就提供一个開始ES插件开发演示样例,ES版本号为1.5.2. 一.插件类继承自org.elasticsearch.plugins.AbstractPlugin package org.elasticsearch.plugin.helloworld; import java.util.ArrayList; import java.util.Collection; import java.util.

  • Elasticsearch.Net使用教程 MVC4图书管理系统(2)

    本文实例为大家分享了MVC4图书管理系统的制作教程,供大家参考,具体内容如下 首先项目结构图: Model层的相关代码如下: Book.cs代码如下: public class Book { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } [MaxLength(500)] [Display(Name = "标题")] public string Title

  • 基于Lucene的Java搜索服务器Elasticsearch安装使用教程

    一.安装Elasticsearch Elasticsearch下载地址:http://www.elasticsearch.org/download/ ·下载后直接解压,进入目录下的bin,在cmd下运行elasticsearch.bat 即可启动Elasticsearch ·用浏览器访问: http://localhost:9200/   ,如果出现类似如下结果则说明安装成功: { "name" : "Benedict Kine", "cluster_na

  • 详解centos7虚拟机安装elasticsearch5.0.x-安装篇

    centos7虚拟机安装elasticsearch5.0.x-安装篇 请预先安装jdk详细步骤请参考:http://www.jb51.net/softjc/193398.html 创建新用户(非root用户) elasticsearch只能用非root启动,这里我创建了一个叫seven的用户 [root@localhost ~]# useradd seven [root@localhost ~]# passwd seven 下载elasticsearch [root@localhost ~]#

  • Elasticsearch.Net使用入门教程(1)

    本文实例为大家分享了Elasticsearch.Net使用教程,供大家参考,具体内容如下 首先去官网下载Elasticsearch 2.3.4安装包,解压后,在cmd命令行进入安装目录,再进入 bin目录,运行elasticsearch.bat命令. elasticsearch插件elasticsearch-head安装: bin目录下执行命令plugin -install mobz/elasticsearch-head 然后开始.net编程,构建控制台应用程序 Program.cs代码如下:

  • SpringBoot整合ElasticSearch实践

    本节我们基于一个发表文章的案例来说明SpringBoot如何elasticsearch集成.elasticsearch本身可以是一个独立的服务,也可以嵌入我们的web应用中,在本案例中,我们讲解如何将elasticsearch嵌入我们的应用中. 案例背景:每个文章(Article)都要属于一个教程(Tutorial),而且每个文章都要有一个作者(Author). 一.实体设计: Tutorial.java public class Tutorial implements Serializable

  • 详解spring-boot集成elasticsearch及其简单应用

    介绍 记录将elasticsearch集成到spring boot的过程,以及一些简单的应用和helper类使用. 接入方式 使用spring-boot中的spring-data-elasticsearch,可以使用两种内置客户端接入 1.节点客户端(node client): 配置文件中设置为local:false,节点客户端以无数据节点(node-master或node-client)身份加入集群,换言之,它自己不存储任何数据,但是它知道数据在集群中的具体位置,并且能够直接转发请求到对应的节

  • centos下root运行Elasticsearch异常问题解决

     在CentOS 6.5 上运行Elasticsearch 2.3,异常如下: Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root. at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93) at org.elasticsearch.bootstrap.Bootstra

随机推荐