zl程序教程

您现在的位置是:首页 >  Java

当前栏目

SpringDataElasticsearch操作Elasticsearch自定义方法查询

2023-02-18 16:35:50 时间

准备工作

对应 准备 里操作,导包,实体类的

接口

需要这个接口,如果这篇 文章 跟着做了,则不需要这个接口,文章里已经定义好了,

import com.itcsdn.pojo.Esneo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
//ElasticsearchRepository<对应类的类名,对应类的id类型>
public interface GoodsRepository extends ElasticsearchRepository<Esneo,Long> {
}

定义方法

只要遵循SpringData提供的语法,我们可以任意定义方法声明 不用实现该方法 在接口中添加定义方法:

import com.itcsdn.pojo.Esneo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;

//ElasticsearchRepository<对应类的类名,对应类的id类型>
public interface GoodsRepository extends ElasticsearchRepository<Esneo,Long> {

    /**自定义方法:
     * 根据价格区间查询
     * Price: 是实体类属性名,这里是价格属性
     * @param from 开始价格
     * @param to 结束价格
     * @return 符合条件的goods
     */
    List<Esneo> findByPriceBetween(Double from, Double to);
}

调用代码:

import com.itcsdn.es.GoodsRepository;
import com.itcsdn.pojo.Goods;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

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

    @Autowired //注入刚才创建的接口
    private GoodsRepository goodsRepository;

    /**
     *  根据价格区间查询
     */
    @Test
    public void testFind(){
        //调用定义的方法
        List<Esneo> between = goodsRepository.findByPriceBetween(1000d, 4000d);
        //打印结果
        between.forEach(System.out::println);
    }

}

支持的一些语法对应表:

Keyword

Sample

Elasticsearch Query String

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(Collection<String>names)

{"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}}

NotIn

findByNameNotIn(Collection<String>names)

{"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}}

Near

findByStoreNear

Not Supported Yet !

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}}}}