You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Data Elasticsearch请求中添加ignore_unmapped自定义属性

方案1:Spring Data Elasticsearch 4.2.x及以上版本(推荐)

4.2及以上版本已经内置了对应参数的配置方法,直接在构造排序规则时调用withUnmappedType(true)即可,框架会自动映射为"ignore_unmapped": true:

// 构造带ignore_unmapped配置的排序规则
Sort sort = Sort.by(
    Sort.Order.asc("title")
        .withUnmappedType(true)
);

// 拼装分页参数
PageRequest pageRequest = PageRequest.of(0, 15, sort);

// 直接调用Repository查询即可
Page<YourDocument> result = yourDocumentRepository.findAll(pageRequest);

方案2:低版本使用原生查询构造

如果你的项目使用的是4.2以下版本,没有内置的配置方法,可以通过NativeSearchQuery手动构造排序参数:

import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

// 手动构造排序规则,设置ignoreUnmapped属性
var titleSort = SortBuilders.fieldSort("title")
    .order(SortOrder.ASC)
    .ignoreUnmapped(true);

// 构造查询请求
NativeSearchQuery query = new NativeSearchQueryBuilder()
    .withPageable(PageRequest.of(0, 15))
    .withSorts(titleSort)
    // 可在此处添加你的业务查询条件,例如.withQuery(...)
    .build();

// 执行查询
Page<YourDocument> result = elasticsearchOperations.search(query, YourDocument.class);

内容的提问来源于stack exchange,提问作者Teddy Kossoko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 05:30:02