JsonData.fromJson(“now”)出现问题,接上文,去年6月份遇到了一个关于JsonData.fromJson(“now”)出现问题,当时思考许久,由于项目需要上线,不得已采用Reader方法来解决。现在已经探索到了解决方案,原来不能使用JsonData.fromJson这个方法,而是应该使用JsonData.of(“now”)这个方法来解决。下面为代码的编写方式,供大家参考。
private SearchResponse<Map> searchBySplitWord(PageQueryParam<PageQueryDTO> pageQueryParam) {
SearchResponse<Map> searchResponse;
try {
searchResponse = elasticsearchClient.search(srBuilder -> srBuilder
.index("business-weekly-article-alias")
.minScore(SystemConstant.MIN_SCORE)
// MultiMatch 查找:对输入内容先分词再查询。
.query(builder -> builder
.functionScore(functionScoreQueryBuilder -> functionScoreQueryBuilder
.query(queryBuilder -> queryBuilder
.disMax(disMaxQueryBuilder -> disMaxQueryBuilder
.boost(1.2F)
.tieBreaker(0.7)
.queries(queryListBuilder -> queryListBuilder
.multiMatch(multiMatchQueryBuilder -> multiMatchQueryBuilder
.fields("keywords^20", "title^15", "content^5", "author^5", "smallTitle^15")
.query(pageQueryParam.getParam().getKeywords())
.type(TextQueryType.BestFields)
.tieBreaker(0.7)
.minimumShouldMatch("3<90%")
)
)
))
.boostMode(FunctionBoostMode.Sum)
.scoreMode(FunctionScoreMode.Sum)
.functions(functionQueryBuilder -> functionQueryBuilder
.gauss(decayFunction -> decayFunction
.field("createTime")
.placement(placementBuilder -> placementBuilder
.origin(JsonData.of("now"))
.offset(JsonData.of("30d"))
.scale(JsonData.of("9000d"))
.decay(0.5)
)
)
.weight(20D)
))
)
// 高亮查询
.highlight(highlightBuilder -> highlightBuilder
.preTags("<font color='red'>")
.postTags("</font>")
.requireFieldMatch(false) //多字段时,需要设置为false
.fields("keywords", highlightFieldBuilder -> highlightFieldBuilder)
.fields("title", highlightFieldBuilder -> highlightFieldBuilder)
.fields("content", highlightFieldBuilder -> highlightFieldBuilder.fragmentSize(1000).numberOfFragments(0))
.fields("author", highlightFieldBuilder -> highlightFieldBuilder)
.fields("smallTitle", highlightFieldBuilder -> highlightFieldBuilder)
)
.from(pageQueryParam.getPageNo() * pageQueryParam.getPageSize())
.size(pageQueryParam.getPageSize())
.sort(sortOptionsBuilder -> sortOptionsBuilder
.field(fieldSortBuilder -> fieldSortBuilder
.field("_score").order(SortOrder.Desc)
/*.field("publishedAt").order(SortOrder.Desc)*/))
, Map.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
return searchResponse;
}