在 Mapping 里面,将 dynamic 参数设置成 strict 可以拒绝索引包含未知字段的文档。 此条 Tips 由 medcl 贡献。

SpringBoot整合ES搜索结果部分字段为null

Elasticsearch | 作者 Sakura0408 | 发布于2020年04月08日 | 阅读数:1910

请求代码:
public BaseResponse<JSONObject> search(String search, Integer page, Integer size) {
if (page < 1) {
page = 1;
}
if (size < 10) {
size = 10;
}
PageRequest pageRequest = PageRequest.of(page, size);
BoolQueryBuilder builder = QueryBuilders.boolQuery();
builder.must(QueryBuilders.multiMatchQuery(search, "title", "content", "tags"));
Page<Article> searchResult = articleRepository.search(builder, pageRequest);
return setResultSuccess((JSONObject) JSON.toJSON(searchResult));
}
响应数据:
{
"code": 200,
"msg": "success",
"data": {
"last": true,
"pageable": {
"paged": true,
"pageNumber": 1,
"offset": 10,
"pageSize": 10,
"unpaged": false,
"sort": {
"unsorted": true,
"sorted": false,
"empty": true
}
},
"sort": {
"unsorted": true,
"sorted": false,
"empty": true
},
"maxScore": 0.13959092,
"content": [
{
"createTime": null,
"goodCount": null,
"author": 1,
"collectCount": null,
"updateTime": null,
"id": 16,
"viewCount": null,
"title": " 我是测试文章15号",
"content": " 我是测试文章15号",
"commentCount": null,
"tags": "1,14,10"
},
{
"createTime": null,
"goodCount": null,
"author": 1,
"collectCount": null,
"updateTime": null,
"id": 17,
"viewCount": null,
"title": "我是测试文章16号",
"content": "我是测试文章16号",
"commentCount": null,
"tags": "1,14,10"
},
{
"createTime": null,
"goodCount": null,
"author": 1,
"collectCount": null,
"updateTime": null,
"id": 9,
"viewCount": null,
"title": "我是测试文章8号",
"content": "我是测试文章8号",
"commentCount": null,
"tags": "1,14,10"
},
{
"createTime": null,
"goodCount": null,
"author": 1,
"collectCount": null,
"updateTime": null,
"id": 10,
"viewCount": null,
"title": "我是测试文章9号",
"content": "我是测试文章9号",
"commentCount": null,
"tags": "1,14,10"
},
{
"createTime": null,
"goodCount": null,
"author": 1,
"collectCount": null,
"updateTime": null,
"id": 2,
"viewCount": null,
"title": "我是测试文章1号",
"content": "我是测试文章1号内容",
"commentCount": null,
"tags": "1,14,10"
},
{
"createTime": null,
"goodCount": null,
"author": 1,
"collectCount": null,
"updateTime": null,
"id": 3,
"viewCount": null,
"title": "我是测试文章2号",
"content": "我是测试文章2号内容",
"commentCount": null,
"tags": "1,14,10"
}
],
"empty": false,
"facets": [],
"scrollId": null,
"totalElements": 16,
"number": 1,
"numberOfElements": 6,
"size": 10,
"totalPages": 2,
"aggregations": null,
"first": false
}
}
文档映射:
@Document(indexName = "article_latest", type = "_doc", shards = 3, replicas = 1)
public class Article {

@Id
private Integer id;
@Field(name = "title", type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String title;
@Field(name = "content", type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String content;
@Field(name = "author", type = FieldType.Long)
private Integer author;
@Field(name = "comment_count", type = FieldType.Long)
private Integer commentCount;
@Field(name = "view_count", type = FieldType.Long)
private Integer viewCount;
@Field(name = "good_count", type = FieldType.Long)
private Integer goodCount;
@Field(name = "collect_count", type = FieldType.Long)
private Integer collectCount;
@Field(name = "tags", type = FieldType.Text)
private String tags;
@Field(name = "create_time", type = FieldType.Long)
private Long createTime;
@Field(name = "update_time", type = FieldType.Long)
private Long updateTime;

// 此处省略get/set/toString方法
}
已邀请:

taogu - 99后

赞同来自:

路过1

taogu - 99后

赞同来自:

路过!

要回复问题请先登录注册