Elasticsearch7.3.2学习笔记

(一)Elasticsearch 7.x版本注意事项
(1)7.x版本中TransportClient连接方式(已废弃),只能使用restclient。Java推荐使用High-level-rest-client 操作es
(2)7.x版本中去除了type,es6时已经规定每一个index只能有一个type。在es7中_doc作为默认type
(3)7.x版本中api请求方式也随着type的变化而变化

(二)使用REST接口操作es

(1)创建一个 ID 为 1 的新文档,并储存键值对,并为其建立索引,es7默认用_doc作为type。

PUT /customer/_doc/1
{
  "name": "John Doe"
}

(2)查询数据 

GET /customer/_doc/1

(3)删除数据

DELETE /customer/_doc/1

 (4)搜索数据

GET /customer/_search
{
  "query": { "match_all": {} },
 
}

// 搜索并分页
GET /customer/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "name": "John" }
  ],
  "from": 1,
  "size": 10
}

// 搜索字段
GET /customer/_search
{
  "query": { "match": { "name": "John" } }
}

// 使用bool搜索
GET /customer/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "John" } }
      ]
    }
  }
}

(三)使用RestHighLevelClient操作ES


@Slf4j
@Component
public class ESUtils {

    private static RestHighLevelClient restHighLevelClient;

    static {
        //这里如果要用client去访问其他节点,就添加进去
        restHighLevelClient = new RestHighLevelClient(RestClient.builder
                (new HttpHost("localhost", 9200, "http")));
    }
    
    /**
     * 创建索引
     * @param idxName 索引名称
     * @param idxSQL  索引描述
     * @return void
     * @throws
     * @since
     */
    public static CreateIndexResponse createIndex(String idxName, Map idxSQL) {


        try {
            if (isExistsIndex(idxName)) {
                log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL);
                return null;
            }
            CreateIndexRequest request = new CreateIndexRequest(idxName);
            buildSetting(request);
            request.mapping(idxSQL);
//            request.settings() 手工指定Setting
            CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
            if (!res.isAcknowledged()) {
                throw new RuntimeException("初始化失败");
            }
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }


    /**
     * 判断某个index是否存在
     *
     * @param idxName index名
     * @return boolean
     * @throws
     * @since
     */
    public static boolean isExistsIndex(String idxName) throws Exception {
        return restHighLevelClient.indices().exists(new GetIndexRequest(idxName), RequestOptions.DEFAULT);
    }

    /**
     * 设置分片
     *
     * @param request
     * @return void
     * @throws
     * @since
     */
    public static void buildSetting(CreateIndexRequest request) {
        request.settings(Settings.builder().put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2));
    }

    /**
     * 新增和编辑数据
     * @param idxName
     * @param id
     * @param data
     * @return
     */
    public static IndexResponse insertOrUpdateOne(String idxName, String id, Map data) {
        
        IndexRequest request = new IndexRequest(idxName);
        log.error("Data : id={},entity={}",id, JSON.toJSONString(data));
        request.id(id);
        request.source(data, XContentType.JSON);
        try {
            return restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 批量插入数据
     *
     * @param idxName index
     * @param list    带插入列表
     * @return void
     * @throws
     * @since
     */
    public static BulkResponse insertBatch(String idxName, List list) {
        BulkRequest request = new BulkRequest();

        list.forEach(item -> request.add(new IndexRequest(idxName).id(item.getId())
                .source(item.getData(), XContentType.JSON)));
        try {
            return restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除
     *
     * @param idxName index
     * @param idList  待删除列表
     * @return void
     * @throws
     * @since
     */
    public static BulkResponse deleteBatch(String idxName, Collection idList) {
        BulkRequest request = new BulkRequest();
        idList.forEach(item -> request.add(new DeleteRequest(idxName, item.toString())));
        try {
            return restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除
     *
     * @param idxName index
     * @param id      待删除ID
     * @return void
     * @throws
     * @since
     */
    public static DeleteResponse deleteOne(String idxName, String id) {
        DeleteRequest request = new DeleteRequest(idxName, id);
        try {
            return restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * @param idxName index
     * @param builder 查询参数
     * @param c       结果类对象
     * @return java.util.List
     * @throws
     * @since
     */
    public static  List search(String idxName, SearchSourceBuilder builder, Class c) {
        SearchRequest request = new SearchRequest(idxName);
        request.source(builder);
        try {
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            List res = new ArrayList<>(hits.length);
            log.info(response.toString());

            for (SearchHit hit : hits) {
                res.add(JSON.parseObject(hit.getSourceAsString(), c));
            }
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除index
     *
     * @param idxName
     * @return void
     * @throws
     * @since
     */
    public static AcknowledgedResponse deleteIndex(String idxName) {
        try {
            if (isExistsIndex(idxName)) {
                log.error(" idxName={} 已经存在", idxName);
                throw new RuntimeException("idxName已经存在");
            }
            return restHighLevelClient.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * @param idxName
     * @param builder
     * @return void
     * @throws
     * @since
     */
    public static BulkByScrollResponse deleteByQuery(String idxName, QueryBuilder builder) {

        DeleteByQueryRequest request = new DeleteByQueryRequest(idxName);
        request.setQuery(builder);
        //设置批量操作数量,最大为10000
        request.setBatchSize(10000);
        request.setConflicts("proceed");
        try {
            return restHighLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
}
 
  

(四)什么是倒排索引

在搜索引擎中,每个文档都有一个对应的文档 ID,文档内容被表示为一系列关键词的集合。例如,文档 1 经过分词,提取了 20 个关键词,每个关键词都会记录它在文档中出现的次数和出现位置。那么,倒排索引就是关键词到文档 ID 的映射,每个关键词都对应着一系列的文件,这些文件中都出现了关键词。搜索系统查找倒排索引,从中读出包含这个单词的文档,这些文档就是提供给用户的搜索结果。

参考https://blog.csdn.net/abcd1101/article/details/89010070

你可能感兴趣的:(架构师)