欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring data Jpa操作ES

程序员文章站 2022-05-01 23:11:56
...

Spring data Jpa操作ES

在ElasticsearchRepository中我们可以使用Not Add Like Or Between等关键词自动创建查询语句。

记住上面这句话,代码示例:

public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> {

    /**
     * 通过ID字段查询
     */
    Employee queryEmployeeById(String id);

}

打开这个类我们发现:

@NoRepositoryBean
public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> {
    <S extends T> S index(S var1);

    Iterable<T> search(QueryBuilder var1);

    Page<T> search(QueryBuilder var1, Pageable var2);

    Page<T> search(SearchQuery var1);

    Page<T> searchSimilar(T var1, String[] var2, Pageable var3);

    void refresh();

    Class<T> getEntityClass();
}

那么具体关键字有哪些呢?下面一一列出:

关键字 使用示例 等同于的ES查询
And findByNameAndPrice {“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Or findByNameOrPrice {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Is findByName {“bool” : {“must” : {“field” : {“name” : “?”}}}}
Not findByNameNot {“bool” : {“must_not” : {“field” : {“name” : “?”}}}}
Between findByPriceBetween {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
LessThanEqual findByPriceLessThan {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
GreaterThanEqual findByPriceGreaterThan {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Before findByPriceBefore {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
After findByPriceAfter {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Like findByNameLike {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
StartingWith findByNameStartingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
EndingWith findByNameEndingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “*?”,”analyze_wildcard” : true}}}}}
Contains/Containing findByNameContaining {“bool” : {“must” : {“field” : {“name” : {“query” : “?”,”analyze_wildcard” : true}}}}}
In findByNameIn(Collectionnames) {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}}
NotIn findByNameNotIn(Collectionnames) {“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}}
True findByAvailableTrue {“bool” : {“must” : {“field” : {“available” : true}}}}
False findByAvailableFalse {“bool” : {“must” : {“field” : {“available” : false}}}}
OrderBy findByAvailableTrueOrderByNameDesc {“sort” : [{ “name” : {“order” : “desc”} }],”bool” : {“must” : {“field” : {“available” : true}}}}

那么通过上述我们就可以写出一个简单的例子:

  @Autowired
    private EmployeeRepository employeeRepository; 


 /**
     * 查询
     */
    @RequestMapping("/query/{id}")
    public Employee query(@PathVariable("id") String id) {

        Employee accountInfo = employeeRepository.queryEmployeeById(id);
        System.err.println(accountInfo.toString());

        return accountInfo;
    }
 

/**
     * 聚合查询测试
     */
    @RequestMapping("/querySum")
    public Iterable querySum() {
        /**
         * 创建查询体
         */
        BoolQueryBuilder builder = QueryBuilders.boolQuery();

        /**
         * 设置聚合条件
         */
        RangeQueryBuilder query = QueryBuilders.rangeQuery("age").from("30").to("60");
        
        /**
         * 将聚合条件设置入查询体之中
         */
        builder.must(query);

        Iterable<Employee> search = employeeRepository.search(builder);

        return search;
    }

至此最基本的功能已经完成

相关标签: java elasticsearch