Fork me on GitHub

ElasticSearch-学习笔记03-基本增删改查

 

  • entity
/**
 * Created by lihui on 2019/1/30.
 */
public class SearchEntity implements Serializable {

    private String id;

    private Date createTime;

    public SearchEntity(){}
    public SearchEntity(String id, Date createTime) {
        this.id = id;
        this.createTime = createTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}
SearchEntity.java
package com.huarui.entity;

import java.util.Date;

/**
 * Created by lihui on 2019/1/30.
 */
public class Book extends SearchEntity {

    private String name;

    private String code;

    private float price;

    public Book(){}
    public Book(String id, Date createTime, String code, String name, float price) {
        super(id,createTime);
        this.name = name;
        this.price = price;
        this.code = code;
    }

    public Book(String name, float price, String code) {
        this.name = name;
        this.price = price;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", code='" + code + '\'' +
                ", price=" + price +
                '}';
    }
}
Book.java

 

  • exception
package com.huarui.exection;

/**
 * Created by lihui on 2019/1/30.
 */
public class JestExcetion extends Exception {

    public JestExcetion(){}

    public JestExcetion(String message) {
        super(message);
    }

}
JestExcetion.java

 

  • service
package com.huarui.service;

import com.huarui.entity.SearchEntity;
import com.huarui.exection.JestExcetion;
import io.searchbox.client.JestResult;

/**
 * Created by lihui on 2019/1/30.
 */
public interface JestClientService {

    <T extends SearchEntity> JestResult index(T t, String indexKey) throws JestExcetion;

    JestResult delete(String id)throws JestExcetion;

    <T extends SearchEntity> T get(String id, Class<T> clazz) throws JestExcetion;

    JestResult deleteIndex() throws JestExcetion;

}
JestClientService.java
package com.huarui.service.impl;

import com.huarui.entity.SearchEntity;
import com.huarui.exection.JestExcetion;
import com.huarui.service.JestClientService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Delete;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by lihui on 2019/1/30.
 * JestClient 操作类
 */
@Service
public class JestClientServiceImpl implements JestClientService {

    private static Logger LOGGER = LoggerFactory.getLogger(JestClientServiceImpl.class);

    @Autowired
    private JestClient jestClient;

    private String indexName = "test";//索引名称
    private String indexType = "books";//文档类型

    @Override
    public <T extends SearchEntity> JestResult index(T t, String indexKey) throws JestExcetion{
        JestResult jestResult = null;
        Index index = new Index
                .Builder(t)//文档
                .index(indexName)//索引
                .type(indexType)//文档类型
                .id(indexKey)//key
                .build();
        try {
            jestResult = jestClient.execute(index);
        } catch (IOException e) {
            LOGGER.info(e.getMessage());
            throw new JestExcetion(e.getMessage());
        }
        return jestResult;
    }

    @Override
    public JestResult delete(String id) throws JestExcetion{
        JestResult jestResult = null;
        Delete delete = new Delete.Builder(id)
                .index(indexName)
                .type(indexType)
                .build();
        try {
            jestResult = jestClient.execute(delete);
        } catch (IOException e) {
            LOGGER.info(e.getMessage());
            throw new JestExcetion(e.getMessage());
        }
        return jestResult;
    }

    @Override
    public <T extends SearchEntity> T get(String id, Class<T> clazz) throws JestExcetion{
        JestResult jestResult = null;
        Get get = new Get.Builder(indexName, id)
                .type(indexType)
                .build();
        try {
            jestResult = jestClient.execute(get);
            if (jestResult.isSucceeded()){
                return jestResult.getSourceAsObject(clazz);
            }
        } catch (IOException e) {
            LOGGER.info(e.getMessage());
            throw new JestExcetion(e.getMessage());
        }
        return null;
    }

    @Override
    public JestResult deleteIndex() throws JestExcetion{
        JestResult jestResult = null;
        Delete delete = new Delete.Builder(indexName).build();
        try {
            jestResult = jestClient.execute(delete);
        } catch (IOException e) {
            LOGGER.info(e.getMessage());
            throw new JestExcetion(e.getMessage());
        }
        return jestResult;
    }

}
JestClientServiceImpl.java

 

  • junit

 

package com.huarui.youxiu326;

import com.huarui.entity.Book;
import com.huarui.exection.JestExcetion;
import com.huarui.service.JestClientService;
import com.huarui.service.impl.JestClientServiceImpl;
import io.searchbox.client.JestResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseSearchTest {

    private static Logger LOGGER = LoggerFactory.getLogger(JestClientServiceImpl.class);

    @Autowired
    private JestClientService jestService;

    /**
     * 创建索引
     */
    @Test
    public void testIndex() throws JestExcetion {

        Book book = new Book("326",new Date(),"youxiu326","创建一个索引",3.26F);

        JestResult index = jestService.index(book, book.getId());
        if (index.isSucceeded()){
            LOGGER.info("创建成功了");
        }
    }

    /**
     * 根据id查询索引
     */
    @Test
    public void testGet() throws JestExcetion {

        Book book = jestService.get("326", Book.class);
        System.out.println(book);

    }

    /**
     * 根据id删除索引
     */
    @Test
    public void testDelete() throws JestExcetion {

        JestResult delete = jestService.delete("326");
        if (delete.isSucceeded()){
            LOGGER.info("删除成功");
        }else {
            LOGGER.info("删除失败");
        }

    }
}

 

 

posted @ 2019-04-06 19:52  youxiu326  阅读(205)  评论(1编辑  收藏  举报