Spring Data Elasticsearch 进行查询时忽略大小写

本文详细介绍了如何在Spring Boot项目中使用Elasticsearch进行文本搜索,包括配置依赖、定义POJO类、设置索引和字段类型,以及执行高亮查询的过程。通过具体代码示例,展示了如何构建查询条件、解析搜索结果,并获取高亮显示的匹配项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!-- elasticsearch-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

<!-- elasticsearch-->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>3.2.6.RELEASE</version>
</dependency>

elasticsearch版本:6.8.4

pojo类

@Document(indexName = "jd", type = "item") //指定索引(数据库)、类型(数据表)
@Setting(settingPath = "elastic-setting.json")
@Data
public class ItemByEs {
    @Id
    private String id;

    @Field(type = FieldType.Keyword, normalizer = "string_lowercase")//指定类型、使用的分词器
    private String title; //标题

    @Field(type = FieldType.Keyword)
    private String category;// 分类

    @Field(type = FieldType.Keyword)
    private String brand; // 品牌

    @Field(type = FieldType.Double)
    private Double price; // 价格

    @Field(index = false, type = FieldType.Keyword) //index = false
    private String images; // 图片地址
}

  elastic-setting.json

{
  "analysis": {
    "normalizer": {
      "string_lowercase": {
        "type": "custom",
        "filter": [
          "lowercase"
        ]
      }
    }
  }
}

 

query:

    QueryBuilder matchQuery = QueryBuilders.matchQuery("title", itemByEsVo.getTitle())
//                .analyzer("caseSensitive")
                .operator(Operator.OR);//or查询 Operator.OR、and查询Operator.AND


SearchResponse response = client.prepareSearch("jd")
        .setQuery(matchQuery)
        .highlighter(highlightBuilder)
        // 设置一次返回的文档数量
        .setSize(10)
        .get();

结果:

Index:jd
Type:item
Id:1
title: huawei1
Scope:2.5389738
<span style="color:red;">huawei1</span>
Source As String:{"id":"51","title":"HuaWei1","category":"手机","brand":"小米","price":3550.0,"images":"http://image.baidu.com/13123.jpg"}
Source As Map:{images=http://image.baidu.com/13123.jpg, price=3550.0, id=51, title=HuaWei1, category=手机, brand=小米}
Index:jd
Type:item
Id:51
title: HuaWei1
Scope:2.4849067
<span style="color:red;">HuaWei1</span>
[{Source As String={"id":"1","title":"huawei1","category":"手机","brand":"华为","price":3500.0,"images":"http://image.baidu.com/13123.jpg"}, Source As Map={images=http://image.baidu.com/13123.jpg, price=3500.0, id=1, title=huawei1, category=手机, brand=华为}, Index=jd, Type=item, Id=1, Title=huawei1, Scope=2.5389738, Highlight=<span style="color:red;">huawei1</span>}, {Source As String={"id":"51","title":"HuaWei1","category":"手机","brand":"小米","price":3550.0,"images":"http://image.baidu.com/13123.jpg"}, Source As Map={images=http://image.baidu.com/13123.jpg, price=3550.0, id=51, title=HuaWei1, category=手机, brand=小米}, Index=jd, Type=item, Id=51, Title=HuaWei1, Scope=2.4849067, Highlight=<span style="color:red;">HuaWei1</span>}]
共查出2条记录!
共耗时8ms
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值