spring cloud如何集成elasticsearch

一、导入elasticsearch依赖

其他依赖自行导入

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-elasticsearchartifactId>
        dependency>
    dependencies>

二、在相对应的启动类添加elasticsearch开启注解

@Import({Knife4jConfiguration.class}) //Swagger配置文件
@EnableElasticsearchRepositories
@SpringCloudApplication
public class SearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(SearchApplication.class,args);
    }
}

三、创建对应的实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "person")
public class Person {

    @Id
    private String id;

    @Field(value = "name",type = FieldType.Text)
    private String name;

    @Field(value = "address",type = FieldType.Text)
    private String address;
    ///usr/share/elasticsearch/plugins
}

四、实现elasticsearch相关业务接口

@Repository
//CrudRepository不能进行分页,分页可继承PagingAndSortingRepository
public interface PersonRepositories extends CrudRepository<Person,String> {
}

五、测试

@SpringBootTest
public class EsTest {

    @Autowired
    ElasticsearchRestTemplate restTemplate;

    @Autowired
    PersonRepositories personRepositories;

    /**
     * 创建索引并插入数据数据
     */
    @Test
    public void test5(){
        Person person=new Person();
        person.setId("5");
        person.setName("张三");
        person.setAddress("xxxxx");
        personRepositories.save(new Person());
        log.info("操作成功");
    }

    /**
     * 获取数据
     */
    @Test
    public void test4(){
        /**
         *    Optional findById(ID var1);  根据id查询数据
         *
         *     boolean existsById(ID var1);  判断对应id是否存在
         *
         *     Iterable findAll();  查询所有数据
         */

        Iterable<Person> people = personRepositories.findAll();
        people.forEach(item->{
            System.out.println(item);
        });
        log.info("操作成功");
    }

    /**
     * 修改数据
     */
    @Test
    public void test3(){
        Person person = personRepositories.findById("5").get();
        person.setName("小明");
        personRepositories.save(person); //save id存在就修改,不存在就添加
        log.info("操作成功");
    }

    /**
     * 删除数据
     */
    @Test
    public void test2(){
        /**
         *    void deleteById(ID var1); //根据id删除
         *
         *     void delete(T var1); //删除整个对应实体
         *
         *     void deleteAll(Iterable var1); //批量删除
         *
         *     void deleteAll();  //删除所有
         */
        personRepositories.deleteById("5");
        log.info("操作成功");
    }

    /**
     * 自定义方法
     */
    @Test
    public void test1(){
     /**
     * Spring Data 的另一个强大功能,是根据方法名称自动实现功能。
	 * 比如:你的方法名叫做:findByTitle,那么它就知道你是根据title查询,
	 * 然后自动帮你完成,无需写实现类。
	 * 当然,方法名称要符合一定的约定:
     *
     */  
    }
}

官方命名模板

Keyword Sample Elasticsearch Query String
And findByNameAndPrice { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } }, { “query_string” : { “query” : “?”, “fields” : [ “price” ] } } ] } }}
Or findByNameOrPrice { “query” : { “bool” : { “should” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } }, { “query_string” : { “query” : “?”, “fields” : [ “price” ] } } ] } }}
Is findByName { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } } ] } }}
Not findByNameNot { “query” : { “bool” : { “must_not” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] } } ] } }}
Between findByPriceBetween { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : ?, “include_lower” : true, “include_upper” : true } } } ] } }}
LessThan findByPriceLessThan { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : null, “to” : ?, “include_lower” : true, “include_upper” : false } } } ] } }}
LessThanEqual findByPriceLessThanEqual { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : null, “to” : ?, “include_lower” : true, “include_upper” : true } } } ] } }}
GreaterThan findByPriceGreaterThan { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : null, “include_lower” : false, “include_upper” : true } } } ] } }}
GreaterThanEqual findByPriceGreaterThan { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : null, “include_lower” : true, “include_upper” : true } } } ] } }}
Before findByPriceBefore { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : null, “to” : ?, “include_lower” : true, “include_upper” : true } } } ] } }}
After findByPriceAfter { “query” : { “bool” : { “must” : [ {“range” : {“price” : {“from” : ?, “to” : null, “include_lower” : true, “include_upper” : true } } } ] } }}
Like findByNameLike { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?*”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }}
StartingWith findByNameStartingWith { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?*”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }}
EndingWith findByNameEndingWith { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “*?”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }}
Contains/Containing findByNameContaining { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “?”, “fields” : [ “name” ] }, “analyze_wildcard”: true } ] } }}
In (when annotated as FieldType.Keyword) findByNameIn(Collectionnames) { “query” : { “bool” : { “must” : [ {“bool” : {“must” : [ {“terms” : {“name” : [“?”,“?”]}} ] } } ] } }}
In findByNameIn(Collectionnames) { “query”: {“bool”: {“must”: [{“query_string”:{“query”: “”?" “?”", “fields”: [“name”]}}]}}}
NotIn (when annotated as FieldType.Keyword) findByNameNotIn(Collectionnames) { “query” : { “bool” : { “must” : [ {“bool” : {“must_not” : [ {“terms” : {“name” : [“?”,“?”]}} ] } } ] } }}
NotIn findByNameNotIn(Collectionnames) {“query”: {“bool”: {“must”: [{“query_string”: {“query”: “NOT(”?" “?”)", “fields”: [“name”]}}]}}}
True findByAvailableTrue { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “true”, “fields” : [ “available” ] } } ] } }}
False findByAvailableFalse { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “false”, “fields” : [ “available” ] } } ] } }}
OrderBy findByAvailableTrueOrderByNameDesc { “query” : { “bool” : { “must” : [ { “query_string” : { “query” : “true”, “fields” : [ “available” ] } } ] } }, “sort”:[{“name”:{“order”:“desc”}}] }
Exists findByNameExists {“query”:{“bool”:{“must”:[{“exists”:{“field”:“name”}}]}}}
IsNull findByNameIsNull {“query”:{“bool”:{“must_not”:[{“exists”:{“field”:“name”}}]}}}
IsNotNull findByNameIsNotNull {“query”:{“bool”:{“must”:[{“exists”:{“field”:“name”}}]}}}
IsEmpty findByNameIsEmpty {“query”:{“bool”:{“must”:[{“bool”:{“must”:[{“exists”:{“field”:“name”}}],“must_not”:[{“wildcard”:{“name”:{“wildcard”:“*”}}}]}}]}}}
IsNotEmpty findByNameIsNotEmpty {“query”:{“bool”:{“must”:[{“wildcard”:{“name”:{“wildcard”:“*”}}}]}}}

官方连接地址:https://docs.spring.io/spring-data/elasticsearch/docs/4.4.6/reference/html/#elasticsearch.repositories

你可能感兴趣的:(elasticsearch,spring,cloud,java)