ElasticSearch 构建简单的搜索引擎 实现模糊查询并高亮显示

环境搭建

首先对环境进行搭建–引入依赖

核心依赖如下(其他依赖自行引入)

<!--注意:springboot版本-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
</parent>

<!--springboot web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--通过spring data 操作Es-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<!--springboot 继承test-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<!--引入lombook-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
    <scope>provided</scope>
</dependency>

注意:低版本的springboot 不支持springboot data

开发搜索方法

创建实体类:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "shop", type = "product")
public class Product {
    @Id
    private String pid;
    // type 指定数据类型   test支持分词  analyzer 指定分词  searchAnalyzer 指定搜索分词器
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    private String pname;
    @Field(type = FieldType.Double)
    private Double marketprice;
    @Field(type = FieldType.Double)
    private Double shopprice;
    @Field(type = FieldType.Text)
    private String image;
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    private String description;
    @Field(type = FieldType.Integer)
    private Integer ishot;
}

编写yml配置信息

    spring:
      data:
        elasticsearch:
          cluster-nodes: 172.16.251.142:9300   ##指定连接ip地址和端口号

编写简单Repository接口类


public interface ProductRepository extends ElasticsearchRepository<Product, String> {
	//根据需要自己定义方法接口
    List<Product> findProductByMarketpr
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它提供了全文搜索、实时数据分析和数据可视化等功能。在Elasticsearch实现全文搜索高亮显示可以通过以下步骤来完成: 1. 创建索引:首先,你需要创建一个索引来存储你的文档数据。索引是Elasticsearch中用于组织和存储数据的逻辑容器。你可以使用Elasticsearch的API或者客户端库来创建索引。 2. 定义映射:在创建索引之前,你需要定义文档的映射(mapping)。映射定义了文档中的字段以及每个字段的类型。对于需要进行全文搜索和高亮显示的字段,你可以将其类型设置为"text"。 3. 添加文档:将你的文档数据添加到索引中。你可以使用Elasticsearch的API或者客户端库来添加文档。 4. 执行搜索:使用Elasticsearch的搜索API来执行全文搜索。你可以指定搜索条件、过滤条件和排序规则等。在搜索结果中,Elasticsearch会返回匹配的文档以及相关的元数据。 5. 高亮显示:为了实现全文搜索高亮显示,你可以使用Elasticsearch的高亮功能。在搜索请求中,你可以指定需要高亮显示的字段以及高亮显示的样式。在搜索结果中,Elasticsearch会将匹配的关键词用指定的样式进行标记。 下面是一个示例的搜索请求,用于实现全文搜索高亮显示: ``` GET /your_index/_search { "query": { "match": { "your_field": "your_query" } }, "highlight": { "fields": { "your_field": {} } } } ``` 在上面的请求中,你需要将"your_index"替换为你的索引名称,"your_field"替换为你要搜索的字段名称,"your_query"替换为你的搜索关键词。搜索结果中,匹配的关键词会被用默认的高亮样式进行标记。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值