ElasticSearch 使用java代码,实现批量新增、修改、构建索引 Api操作

前言

ElasticSearch 这里的测试是使用,借助elasticsearch-head 插件和 Elasticsearch IK分词器插件。如果测试可以参考 https://blog.csdn.net/fajing_feiyue/article/details/108901751 来进行安装测试。

API 操作样例

(一) 添加maven依赖

  
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.data
            spring-data-elasticsearch
            3.2.9.RELEASE
        

(二) 配置application.properties文件

spring.data.elasticsearch.cluster-name=my-es
spring.data.elasticsearch.cluster-nodes=ip:9300

(三) 测试创建索引和删除索引

 @Resource
    private ElasticsearchTemplate esTemplate;

    @Test
    public void createIndex() {
    		//创建索引
           esTemplate.createIndex(EsSearchServiceImpl.INDEX_PREFIX + "20201002");
           //添加映射
           esTemplate.putMapping(ItemEsSearch.class);
        
    }

    @Test
    public void deleteIndex() {
        esTemplate.deleteIndex(EsSearchServiceImpl.INDEX_PREFIX + "20201002");
    }

用于设置映射字段

@Document(indexName = "es_index_prefix_20201002", type = "es_type", shards = 1, replicas = 0)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ItemEsSearch {
    @Id
    private String id;

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String describe;


    @Field(type = FieldType.Keyword)
    private String name;

    @Field(type = FieldType.Keyword)
    private String item;

    @Field(type = FieldType.Date)
    private Date serverTime;

    @Field(type = FieldType.Double)
    private Double price;
}

(四) 构建需要新增/修改数据

 public void batchAddDocument(){
        String desc1 = "describe 描述,也是一个基于Lucene的搜索服务器," +
                "它提供了一个分布式多用户能力的全文搜索引擎," +
                "基于RESTful web接口";
        String desc2 = "Elasticsearch是分布式的。不需要其他组件,分发是实时的,被叫做”Push replication";

        String desc3 = "Elasticsearch 完全支持 Apache Lucene 的接近实时的搜索";

        String desc4 = "处理多租户(multitenancy)不需要特殊配置,而Solr则需要更多的高级设置";
        List descList = new ArrayList<>();
        descList.add(desc1);
        descList.add(desc2);
        descList.add(desc3);
        descList.add(desc4);


        int size = descList.size();

        List itemEsSearchList = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            ItemEsSearch itemEsSearch1 = ItemEsSearch.builder().describe(descList.get(i % size) + i)
                    .item("item" + i)
                    .name("name" + i)
                    .serverTime(new Date()).build();

            itemEsSearchList.add(itemEsSearch1);
        }

        searchService.batchAddDocument(itemEsSearchList,EsSearchServiceImpl.INDEX_PREFIX + "20201003");

    }

新增:

  @Override
    public void batchAddDocument(List itemEsSearchList, String indexName) {
        int counter = 0;

        //判断index 是否存在
        if (!elasticsearchTemplate.indexExists(indexName)) {
             elasticsearchTemplate.createIndex(indexName);
               //添加映射
           esTemplate.putMapping(ItemEsSearch.class);
        }

        Gson gson = new GsonBuilder().setDateFormat("YYYY-MM-dd'T'HH:mm:ss").create();
        List queries = new ArrayList<>(itemEsSearchList.size() > 500 ? 500 : itemEsSearchList.size());
        if(itemEsSearchList != null && itemEsSearchList.size()>0){
            for (ItemEsSearch itemEsSearch : itemEsSearchList) {
                IndexQuery indexQuery = new IndexQuery();
                indexQuery.setId(itemEsSearch.getId());
                indexQuery.setSource(gson.toJson(itemEsSearch));
                indexQuery.setIndexName(indexName);
                indexQuery.setType(INDEX_TYPE);
                queries.add(indexQuery);
                //分批提交索引
                if (counter != 0 && counter % 500 == 0) {
                    elasticsearchTemplate.bulkIndex(queries);
                    queries.clear();
                    log.info("bulkIndex counter :{} indexName is {} ", counter, indexName);
                }
                counter++;
            }

        }
        //不足批的索引最后不要忘记提交
        if (queries.size() > 0) {
            elasticsearchTemplate.bulkIndex(queries);
            log.info("bulkIndex counter :{} indexName is {} ", counter, indexName);
        }
        elasticsearchTemplate.refresh(indexName);

    }

新增效果:
ElasticSearch 使用java代码,实现批量新增、修改、构建索引 Api操作_第1张图片

修改:

  @Test
    public void batchUpdate(){
        List itemEsSearchList = new ArrayList<>();

        String id1 = "dsay7XQBZyMji4yLQ9Pz";
        String id2 = "d8ay7XQBZyMji4yLRNOh";
        String id3 = "eMay7XQBZyMji4yLRNOh";
       // String id4 = "ecay7XQBZyMji4yLRNOh";

        List idList = new ArrayList<>();
        idList.add(id1);
        idList.add(id2);
        idList.add(id3);
        idList.add(null);

        for (int i = 0; i < 4; i++) {
            ItemEsSearch itemEsSearch1 = ItemEsSearch.builder().id(idList.get(i)).describe("新更改描述" + i)
                    .item("item_update" + i)
                    .name("name_update" + i)
                    .serverTime(new Date()).build();

            itemEsSearchList.add(itemEsSearch1);
        }
        searchService.batchAddDocument(itemEsSearchList,EsSearchServiceImpl.INDEX_PREFIX + "20201003");
    }

查看修改效果,可以看到没有设置id新增,设置id修改
ElasticSearch 使用java代码,实现批量新增、修改、构建索引 Api操作_第2张图片

(五)删除数据
测试代码

 @Test
    public void deleteDocument(){

        searchService.deleteDocument("dsay7XQBZyMji4yLQ9Pz",EsSearchServiceImpl.INDEX_PREFIX + "20201003");

    }

具体删除代码

    @Override
    public void deleteDocument(String id, String indexName) {
        DeleteResponse deleteResponse = elasticsearchTemplate.getClient().prepareDelete(indexName, INDEX_TYPE, id).get();
        log.info("delete data {}", new Gson().toJson(deleteResponse));
    }

你可能感兴趣的:(大数据,elasticSearch,java,新增,修改)