ElasticSearch配置及安装

步骤一

先确定已经安装jdk环境,因为ElasticSearch是用Java语言编写的,所以必须安装JDK的环境,并且是JDK 1.8以上。
步骤二
官网下载elasticSearch,地址:https://www.elastic.co/cn/downloads/elasticsearch
解压完成后来到bin路径,双击elasticsearch.bat就启动了。
浏览器访问:http://127.0.0.1:9200/ 有看到如下信息代表启动成功
{
  "name" : "c5472f248c69",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "F0WyBtgmTvaN7h_Kv1q_vA",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
步骤三
安装ElasticSearch-head 
elasticsearch-head是elasticsearch的一款可视化工具,依赖于node.js ,所以需要先安装node.js
GitHub地址:https://github.com/mobz/elasticsearch-head
下载压缩包解压切到工作路径
npm install
npm run start
浏览器访问http://localhost:9100/
![](http://showdoc/server/../Public/Uploads/2023-08-15/64db438ec56d4.png)
配置跨域
修改 Elasticsearch 安装目录中config 文件夹下 elasticsearch.yml 文件,加入下面两行:
http.cors.enabled: true
http.cors.allow-origin: "*"

步骤四 集成项目 以spring boot为例

一、引入依赖

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
 

二、引入依赖配置yml文件加入配置

Spring配置

spring:
elasticsearch:
cluster-nodes: localhost:9200

复制public class BookController {
@Resource
private BaseElasticService elasticService;

@Anonymous
@GetMapping("/query")
public List<Book> query() {
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    TermQueryBuilder termQuery = QueryBuilders.termQuery("author.keyword", "zhy");
    boolQuery.filter(termQuery);
    sourceBuilder.query(boolQuery);
    SearchResponse response = elasticService.search("library", sourceBuilder, null);
    SearchHits hits = response.getHits();
    Book entity = new Book();
    List<Book> booksList = new ArrayList<Book>();
    for (SearchHit hit : hits.getHits()) {
        // 提取字段值
        String title = hit.getSourceAsMap().get("title").toString();
        String idx = hit.getSourceAsMap().get("id").toString();
        String author = hit.getSourceAsMap().get("author").toString();
        entity.setId(idx);
        entity.setTitle(title);
        entity.setAuthor(author);
        booksList.add(entity);
    }
    System.out.println(Arrays.asList(booksList));
    return booksList;

}

@Anonymous
@PostMapping("/createBook")
public String createBook(@RequestBody Book book) {
    ElasticEntity<Book> topicElasticEntity = new ElasticEntity<>("library");
    topicElasticEntity.setId(book.getId());
    topicElasticEntity.setData(book);
    elasticService.insertOrUpdateOne(topicElasticEntity);
    return "OK";
}

@Anonymous
@GetMapping("/removeById")
public String removeById(String id) {
    ElasticEntity<Book> topicElasticEntity = new ElasticEntity<>("library");
    topicElasticEntity.setId(id);
    elasticService.deleteOne(topicElasticEntity);
    return "ok";
}

封装的查询类

复制@Service
@Log4j2
public class BaseElasticService {

public static final Long SCROLL_VALUE_MIN = 5L;
@Resource
RestHighLevelClient client;


/**
 * @param idxName 索引名称
 */
public void createIndex(String idxName) {
    try {
        if (!this.indexExist(idxName)) {
            log.error(" idxName={} 已经存在", idxName);
            return;
        }
        CreateIndexRequest request = new CreateIndexRequest(idxName);
        buildSetting(request);
        //request.mapping(idxSQL, XContentType.JSON);
//            request.settings() 手工指定Setting
        CreateIndexResponse res = client.indices().create(request, RequestOptions.DEFAULT);
        if (!res.isAcknowledged()) {
            throw new RuntimeException("初始化失败");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 判断某个index是否存在
 *
 * @param idxName index名
 * @return boolean
 */
public boolean indexExist(String idxName) throws Exception {
    GetIndexRequest request = new GetIndexRequest(idxName);
    request.local(false);
    request.humanReadable(true);
    request.includeDefaults(false);
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    return client.indices().exists(request, RequestOptions.DEFAULT);
}

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

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

/**
 * @param entity 对象
 */
public void insertOrUpdateOne(ElasticEntity entity) {
    IndexRequest request = new IndexRequest(entity.getIndex());
    log.info("Data : id={},entity={}", entity.getId(), JSON.toJSONString(entity.getData()));
    request.id(entity.getId());
    request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);

    try {
        client.index(request, RequestOptions.DEFAULT);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}


/**
 * 批量插入数据
 *
 * @param list 插入列表
 */
public void insertBatch(List<ElasticEntity> list) {
    BulkRequest request = new BulkRequest();
    list.forEach(item -> request.add(new IndexRequest(item.getIndex()).id(item.getId())
            .source(item.getData(), XContentType.JSON)));
    try {
        client.bulk(request, RequestOptions.DEFAULT);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * 分页游标查询
 *
 * @param idxName index
 * @param builder 查询参数
 */
public SearchResponse search(String idxName, SearchSourceBuilder builder, String scrollId) {
    SearchResponse response = null;
    Scroll scroll = new Scroll(TimeValue.timeValueMinutes(SCROLL_VALUE_MIN));
    try {
        //查询index是否存在
        boolean exists = isExistsIndex(idxName);
        if(!exists){
            createIndex(idxName);
            return null;
        }
        if (StringUtil.isEmpty(scrollId)) {
            //首次查询
            SearchRequest request = new SearchRequest(idxName);
            request.scroll(scroll);
            request.source(builder);
            response = client.search(request, RequestOptions.DEFAULT);
        } else {
            //根据scrollId查询
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(scroll);
            response = client.scroll(scrollRequest, RequestOptions.DEFAULT);
        }

        SearchHit[] hits = response.getHits().getHits();
        if (hits.length == 0) {
            //加载完数据,清除当前scrollId
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            //也可以选择setScrollIds()将多个scrollId一起使用
            clearScrollRequest.addScrollId(response.getScrollId());
            client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
        }

        return response;
    } catch (Exception e) {
        log.error("es分页查询失败", e);
        return null;
    }
}


/**
 * 随机查询查询
 *
 * @param idxName index
 * @param builder 查询参数
 */
public SearchResponse searchRandomly(String idxName, SearchSourceBuilder builder, String scrollId) {
    SearchResponse response = null;
    Scroll scroll = new Scroll(TimeValue.timeValueMinutes(SCROLL_VALUE_MIN));
    try {
        if (StringUtil.isEmpty(scrollId)) {
            //首次查询
            SearchRequest request = new SearchRequest(idxName);
            request.scroll(scroll);
            request.source(builder);
            response = client.search(request, RequestOptions.DEFAULT);
        } else {
            //根据scrollId查询
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(scroll);
            response = client.scroll(scrollRequest, RequestOptions.DEFAULT);
        }

        SearchHit[] hits = response.getHits().getHits();
        if (hits.length == 0) {
            //加载完数据,清除当前scrollId,再随机抽取
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            //也可以选择setScrollIds()将多个scrollId一起使用
            clearScrollRequest.addScrollId(response.getScrollId());
            client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
            //再次重新查询
            SearchRequest request = new SearchRequest(idxName);
            request.scroll(scroll);
            request.source(builder);
            response = client.search(request, RequestOptions.DEFAULT);
        }

        return response;

    } catch (Exception e) {
        log.error("es分页查询失败或游标失效", e);
        //首次查询
        SearchRequest request = new SearchRequest(idxName);
        request.scroll(scroll);
        request.source(builder);
        try {

            response = client.search(request, RequestOptions.DEFAULT);

        } catch (IOException ioException) {
            log.error("es分页查询失败", e);
        }
        return response;
    }
}

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

/**
 * @param entity 对象
 */
public void deleteOne(ElasticEntity entity) {
    DeleteRequest request = new DeleteRequest(entity.getIndex());
    request.id(entity.getId());
    try {
        client.delete(request, RequestOptions.DEFAULT);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}


/**
 * @param idxName
 * @param builder
 */
public void deleteByQuery(String idxName, QueryBuilder builder) {

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值