[英]How to build a query which matches one of the phrases in Spring Elasticsearch repository
I would like to know if and how is possible to create a query which matches one of the keyword phrases and does not contain any of the stop word phrases. 我想知道是否以及如何创建与关键字短语之一匹配且不包含任何停用词短语的查询。 For example:
例如:
List<String> keywords = Arrays.asList("one keyword", "another one");
List<String> stopWords = Arrays.asList("dismiss this");
Page<Result> results = elRepository.findByKeywordsAndStepwords(
keywords, stopwords, new PageRequest(0, 12));
This should match documents containing one of the exact phrases ("one keyword" or "another one") and no stop word phrases ("dismiss this"). 这应该匹配包含精确短语之一(“一个关键字”或“另一个”)和没有停用词短语(“关闭此关键字”)的文档。 Note that if a document contains only terms which are contained in phrases (eg. "another"), it should not return the given document as a result.
请注意,如果文档仅包含短语中包含的术语(例如“另一个”),则不应作为结果返回给定的文档。
After a long research, this was the only way I could achieve my goal. 经过长时间的研究,这是我实现目标的唯一途径。 Hope it will help someone:
希望它能帮助某人:
BoolQueryBuilder keywordBuilder = boolQuery();
keywords.forEach(k -> keywordBuilder.should(matchPhraseQuery("text", k)));
BoolQueryBuilder stopWordBuilder = boolQuery();
stopWords.forEach(s -> stopWordBuilder.should(matchPhraseQuery("text", s)));
BoolQueryBuilder queryBuilder = boolQuery()
.must(keywordBuilder)
.mustNot(stopWordBuilder);
Page<Result> results= elRepository.search(queryBuilder, pageable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.