es的搜索服务

21 篇文章 4 订阅

1、在项目中,创建一个搜索服务的模块:search-service

 2、在新创建的搜索模块中添加依赖:

        <dependencies>
            <!--nacos-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <!--spring-boot提供的mvc-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- es-restclient-操作es-->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
            </dependency>
            <!--feign-远程调用 -->
            <dependency>
                <groupId>com.hmall</groupId>
                <artifactId>feign-api</artifactId>
                <version>1.0</version>
            </dependency>
            <!--spring-boot封装的test测试-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <!--FastJson-字符串和json互相转换-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
            </dependency>
            <!-- amqp-高级消息队列协议 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
            <!--apache提供的工具包-->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
            </dependency>
        </dependencies>

3、配置网关(配置搜索服务路由规则)

指的是在gateway网关模块的配置文件中配置搜索服务路由规则

gateway网关的配置详细可参考

http://t.csdn.cn/ueadT

        
- id: search-service  #路由id,自定义,只要不重复即可
          uri: lb://searchservice  #本路由对应的后台服务
          predicates:
            - Path=/search/**

4、启动es和kibana

安装es和kibana,详情可参考

http://t.csdn.cn/V6vXx

如果是在linux中使用docker已经安装过es和kibana

docker的相关命令可参考

http://t.csdn.cn/KaDih

启动成功后访问:

http://192.168.177.132:5601

5、设计并创建索引库

可以参考

http://t.csdn.cn/fLwxx

6、数据导入

把数据从mysql导入es有两种方式:

第一种:直接在es的代码上查询数据库的数据,用批处理的方式导入

 可以参考

http://t.csdn.cn/oGDGB

这种方式有两个问题:

1.没有做到单一职责,es直接调用数据库

2.如果数据量太大,容易出现问题

可以参考

http://t.csdn.cn/IZt9j

7、创建接收请求参数类

8、自动补全查询

详情也可参考:

http://t.csdn.cn/kW17T

controller层:

@Slf4j
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private IsearchService isearchService;

    /**
     * 搜索栏自动补全
     */
    @GetMapping("/suggestion")
    public List<String> getsSuggestion (@RequestParam ("key") String prefix){
        log.info("搜索栏自动补全===前后端联通==" + prefix);
        return this.isearchService.getsSuggestion(prefix);
    }

service层:

/**
     * 搜索栏自动补全
     */
    List<String> getsSuggestion(String prefix);

service实现类:

    /**
     * 搜索栏自动补全
     */
    @Override
    public List<String> getsSuggestion(String prefix) {
        try {
            // 1.准备Request
            SearchRequest request = new SearchRequest("item");
            // 2.准备DSL
            request.source().suggest(
                    new SuggestBuilder().addSuggestion(
                            "suggestions1",// 查询的名称,自定义
                            SuggestBuilders.completionSuggestion("suggestion")//自动补全的字段名
                                    .prefix(prefix)//关键字,查询以北京开头的数据
                                    .skipDuplicates(true)// 跳过重复词条
                                    .size(10)//显示的词条数量
                    ));
            // 3.发起请求
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            // 4.解析结果
            List<String> list = new ArrayList<>();
            Suggest suggest = response.getSuggest();
            // 4.1.根据补全查询名称,获取补全结果
            CompletionSuggestion suggestion1 = suggest.getSuggestion("suggestions1");
            // 4.2.获取options
            List<CompletionSuggestion.Entry.Option> options = suggestion1.getOptions();
            // 4.3.遍历
            for (CompletionSuggestion.Entry.Option option : options) {
                String term = option.getText().toString();
                list.add(term);
            }
            return list;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

9、过滤项聚合功能

详情可参考

http://t.csdn.cn/M4Q9y

10、实现基本搜索功能

详情可参考

http://t.csdn.cn/L7WEl

11、数据同步

两种方式:两种方法都是用的mq的监听来实现的

第一种:es直接操作数据库

http://t.csdn.cn/XXdiw

第二种:使用feign远程调用的方式操作数据库

http://t.csdn.cn/fBfwn​​​​​​​

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值