微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐