我在elasticsearch“ name”中有一个字段,其中包含: “一些文字” 我想查询与该文本的任何部分匹配的查询,例如“ some”,“ so”或“ xt”
我正在尝试通过queryString
进行此操作:
public Page<View> search(String name, Pageable pageable) {
QueryBuilder queryBuilder = buildQueryFrom(name);
HighlightBuilder highlightBuilder = buildHighlig();
SearchRequestBuilder searchRequest = client.prepareSearch()
.setQuery(queryBuilder)
.highlighter(highlightBuilder)
.setFetchSource(null, new String[]{CV_OCR});
addPageable(searchRequest, pageable);
SearchResponse response = searchRequest.execute().actionGet();
return mapper.mapResponse(response, pageable).map(this::View);
}
private QueryBuilder buildQueryFrom(String text) {
BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.must(queryStringQuery("*" + text + "*").field("name"));
return queryBuilder;
}
但是如果我尝试搜索“%”或其他特殊字符,结果将与我的预期不同。我也在尝试使用正则表达式:
private QueryBuilder buildQueryFrom(String text) {
BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.must(regexpQuery("name", quoteStringAndSetCaseInsensitive(text).pattern()));
return queryBuilder;
}
private Pattern quoteStringAndSetCaseInsensitive(String value) {
return Pattern.compile(Pattern.quote(value), Pattern.CASE_INSENSITIVE);
}
但是没有用。这样搜索还有其他可能性吗?