16.app端文章搜索

这篇具有很好参考价值的文章主要介绍了16.app端文章搜索。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、app端文章搜索

1、搭建ElasticSearch环境

1.1拉取镜像

docker pull elasticsearch:7.4.0

1.2创建容器

docker run -id --name elasticsearch -d --restart=always -p 9200:9200 -p 9300:9300 -v /usr/share/elasticsearch/plugins:/usr/share/elasticsearch/plugins -e "discovery.type=single-node" elasticsearch:7.4.0

1.3 配置中文分词器 ik

因为在创建elasticsearch容器的时候,映射了目录,所以可以在宿主机上进行配置ik中文分词器

在去选择ik分词器的时候,需要与elasticsearch的版本好对应上

#切换目录
cd /usr/share/elasticsearch/plugins
#新建目录
mkdir analysis-ik
cd analysis-ik
#root根目录中拷贝文件
mv elasticsearch-analysis-ik-7.4.0.zip /usr/share/elasticsearch/plugins/analysis-ik
#解压文件
cd /usr/share/elasticsearch/plugins/analysis-ik
unzip elasticsearch-analysis-ik-7.4.0.zip

2.需求分析

  • 用户输入关键可搜索文章列表

  • 关键词高亮显示

  • 文章列表展示与home展示一样,当用户点击某一篇文章,可查看文章详情

思路分析

为了加快检索的效率,在查询的时候不会直接从数据库中查询文章,需要在elasticsearch中进行高速检索。

3.创建索引和映射

使用postman添加映射

put请求 : http://192.168.200.130:9200/app_info_article

{
   
    "mappings":{
   
        "properties":{
   
            "id":{
   
                "type":"long"
            },
            "publishTime":{
   
                "type":"date"
            },
            "layout":{
   
                "type":"integer"
            },
            "images":{
   
                "type":"keyword",
                "index": false
            },
            "staticUrl":{
   
                "type":"keyword",
                "index": false
            },
            "authorId": {
   
                "type": "long"
            },
            "authorName": {
   
                "type": "text"
            },
            "title":{
   
                "type":"text",
                "analyzer":"ik_smart"
            },
            "content":{
   
                "type":"text",
                "analyzer":"ik_smart"
            }
        }
    }
}

16.app端文章搜索

4. 数据初始化到索引库

查询所有的文章信息,批量导入到es索引库中

package com.heima.es;

import com.alibaba.fastjson.JSON;
import com.heima.es.mapper.ApArticleMapper;
import com.heima.es.pojo.SearchArticleVo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
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.List;


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

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    /**
     * 注意:数据量的导入,如果数据量过大,需要分页导入
     * @throws Exception
     */
    @Test
    public void init() throws Exception {
   

        //1.查询所有符合条件的文章数据
        List<SearchArticleVo> searchArticleVos = apArticleMapper.loadArticleList();

        //2.批量导入到es索引库

        BulkRequest bulkRequest = new BulkRequest("app_info_article");

        for (SearchArticleVo searchArticleVo : searchArticleVos) {
   

            IndexRequest indexRequest = new IndexRequest().id(searchArticleVo.getId().toString())
                    .source(JSON.toJSONString(searchArticleVo), XContentType.JSON);

            //批量添加数据
            bulkRequest.add(indexRequest);

        }
        restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);

    }

}

postman查询所有的es中数据 GET请求: http://192.168.200.130:9200/app_info_article/_search文章来源地址https://www.toymoban.com/news/detail-427444.html

5. 文章搜索功能实现

5.1 搭建搜索微服务

1.)添加依赖
<!--elasticsearch-->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.4.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.4.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.4.0</version>
</dependency>
2)nacos配置中心
spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
elasticsearch:
  host: 192.168.200.130
  port: 9200
3)ArticleSearchController
package com.heima.search.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.dto.UserSearchDto;

import com.heima.search.service.ArticleSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/api/v1/article/search")
public class ArticleSearchController {
   


    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * es分页检索
     * @param dto
     * @return
     */
   @PostMapping("/search")
    public ResponseResult articleSerache(@RequestBody UserSearchDto dto) throws IOException {
   
       ResponseResult responseResult = articleSearchService.articleSearch(dto);
       return responseResult;
    }
}

4)Artic

到了这里,关于16.app端文章搜索的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • docker搭建最新ELFK分布式日志收集系统(elasticsearch+logstash+filebeats+kibana7.16.1)

    随着分布式项目的集群部署,日志的存储也分散开来,在日后出现问题进行日志定位时就会出现很困难,服务器很多会做负载均衡,这样最终请求所落在的服务器也随机起来,所以好的方式就是集中收集起来,不需要一台一台服务器去查,方便查看。 ELFK是Elasticsearch+Logstash+F

    2024年02月08日
    浏览(36)
  • Python tkinter 制作文章搜索软件,精准定位想看文章

    前言 嗨喽,大家好呀~这里是爱看美女的茜茜呐 无聊的时候做了一个搜索文章的软件,有没有更加的方便快捷不知道,好玩就行了 环境使用 Python 3.8 Pycharm 模块使用 import requests import tkinter as tk from tkinter import ttk import webbrowser 第三方模块安装方法: win + R 输入cmd 输入安装命令

    2024年02月14日
    浏览(31)
  • Python tkinter 制作文章搜索软件

    今天,我无聊的时候做了一个搜索文章的软件,有没有更加的方便快捷不知道,好玩就行了。基于Python tkinter 制作文章搜索软件,都是一些基础的应用。🍖 🍗 🥩 代码 我们首先做到第一件事是导入模块。 代码 这段代码创建了一个名为 root 的Tkinter窗口对象,并设置了窗口的

    2023年04月08日
    浏览(28)
  • OSPF技术连载16:DR和BDR选举机制,一篇文章搞定!

    你好,这里是网络技术联盟站。 在计算机网络中,开放最短路径优先(Open Shortest Path First,OSPF)是一种广泛使用的内部网关协议(Interior Gateway Protocol,IGP),用于在大型网络中实现路由选择。在OSPF网络中,当一个OSPF区域内有多个路由器时,为了减少链路状态数据库(Link

    2024年02月07日
    浏览(32)
  • 16款开源的全文搜索引擎

    网络安全重磅福利:入门进阶全套282G学习资源包免费分享! 全文搜索引擎就是通过从互联网上提取的各个网站的信息(以网页文字为主)而建立的数据库中,检索与用户查询条件匹配的相关记录,然后按一定的排列顺序将结果返回给用户。 1、Apache Lucene Java 全文搜索框架 许

    2024年02月02日
    浏览(33)
  • ElasticSearch基本使用--ElasticSearch文章一

    https://www.elastic.co/cn/ 1、在当前软件行业中,搜索是一个软件系统或平台的基本功能, 学习ElasticSearch就可以为相应的软件打造出良好的搜索体验。 2、其次,ElasticSearch具备非常强的大数据分析能力。虽然Hadoop也可以做大数据分析,但是ElasticSearch的分析能力非常高,具备Hadoo

    2024年02月14日
    浏览(28)
  • 未被搜索引擎收录的文章:采集难题揭秘

    未被收录的文章能采集吗?这是一个备受争议的话题。作为一名资深网络编辑,我在这里分享一下我的观点和经验。 1.什么是未被收录的文章 2.为什么有些文章未被收录 3.未被收录的文章是否能采集 4.采集未被收录的文章可能面临的问题 5.如何合法采集未被收录的文章 6.为什

    2024年02月03日
    浏览(35)
  • Elasticsearch 整合springboot-Elasticsearch文章二

    https://www.elastic.co/cn/ https://docs.spring.io/spring-data/elasticsearch/docs/4.4.10/reference/html/ 我们选用的是elasticsearch 7.17.9版本,对应的,我们需要升级springboot版本,对应的中间件都需要升级 Springboot: 2.7.10 spring-data-elasticsearch: 4.4.10 spring-boot-starter-data-elasticsearch: 2.7.10 https://github.com/OrderDo

    2024年02月15日
    浏览(31)
  • Elasticsearch 全文检索 分词检索-Elasticsearch文章四

    https://www.elastic.co/guide/en/enterprise-search/current/start.html https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-match-query.html Full text Query中,我们只需要把如下的那么多点分为3大类,你的体系能力会大大提升 很多api都可以查得到,我们只要大概知道有支持哪些功能 Elasticsearch 执行

    2024年02月14日
    浏览(43)
  • ElasticSearch:文章检索

    使用postman添加映射put请求 : 搜索结果展示内容:标题、布局、枫叶图片、发布时间、作者名称、文章id、作者id、静态url 需要对:内容、标题进行分词 json \\\"content\\\":{ \\\"type\\\":\\\"text\\\", \\\"ananlyze\\\":\\\"ik_smart\\\" } http://${url}:${port}/app info article 校验尝试: GET请求查询映射:http://${url}:${port}/ap

    2024年02月07日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包