1. 程式人生 > 其它 >Elasticsearch模糊查詢

Elasticsearch模糊查詢

使用自定義語句進行模糊查詢和使用原生的查詢語句進行查詢

import cn.tedu.mall.pojo.search.entity.SpuForElastic;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * @Author QingHao
 * @Date: 2022/05/17/ 15:36
 * @Describe
 */
@Repository
public interface SpuForElasticRepository extends ElasticsearchRepository<SpuForElastic, Long> {

    //自定義查詢語句
    Iterable<SpuForElastic> querySpuForElasticsByNameMatchesOrTitleMatchesOrDescriptionMatchesOrCategoryNameMatches(
            String name, String title, String description, String categoryName
    );


    // 查詢spu關鍵欄位中包含"手機"的方法
    @Query("{\n" +
            "    \"bool\": {\n" +
            "      \"should\": [\n" +
            "        { \"match\": { \"name\": \"?0\"}},\n" +
            "        { \"match\": { \"title\": \"?1\"}},\n" +
            "        { \"match\": { \"description\": \"?2\"}},\n" +
            "        { \"match\": { \"category_name\": \")3\"}}\n" +
            "        ]\n" +
            "     }\n" +
            "}")
    //當前位置的引數與上面的?0,?1,?2,?3是一1對應的,並不是通過名稱進行匹配
    Iterable<SpuForElastic> querySearch(String name, String title, String description, String categoryName);

}