private void createQuery() {
queryBuilder = new MatchAllQueryBuilder();
// The boolean query builder
BoolQueryBuilder booleanQuery = new BoolQueryBuilder();
// Terms
if (searchTerms != null) {
for (Map.Entry<String, Set<Object>> entry : searchTerms.entrySet()) {
Set<Object> values = entry.getValue();
if (values.size() == 1)
booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
else
booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
}
this.queryBuilder = booleanQuery;
}
// Negative terms
if (negativeSearchTerms != null) {
for (Map.Entry<String, Set<Object>> entry : negativeSearchTerms.entrySet()) {
Set<Object> values = entry.getValue();
if (values.size() == 1)
booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
else
booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
}
this.queryBuilder = booleanQuery;
}
// Date ranges
if (dateRanges != null) {
for (DateRange dr : dateRanges) {
booleanQuery.must(dr.getQueryBuilder());
}
this.queryBuilder = booleanQuery;
}
// Text
if (text != null) {
MatchQueryBuilder textQueryBuilder = QueryBuilders.matchQuery(TEXT, text);
booleanQuery.must(textQueryBuilder);
this.queryBuilder = booleanQuery;
}
// Fuzzy text
if (fuzzyText != null) {
FuzzyLikeThisQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyLikeThisQuery(TEXT_FUZZY).likeText(fuzzyText);
booleanQuery.must(fuzzyQueryBuilder);
this.queryBuilder = booleanQuery;
}
// Fulltext
if (fulltext != null) {
MatchQueryBuilder textQueryBuilder = QueryBuilders.matchQuery(FULLTEXT, fulltext);
booleanQuery.must(textQueryBuilder);
this.queryBuilder = booleanQuery;
}
// Fuzzy fulltext
if (fuzzyFulltext != null) {
FuzzyLikeThisQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyLikeThisQuery(FULLTEXT_FUZZY).likeText(fuzzyFulltext);
booleanQuery.must(fuzzyQueryBuilder);
this.queryBuilder = booleanQuery;
}
// Recency boost. We differentiate between various (random) levels of
// recency
if (recencyBoost) {
Calendar date = Calendar.getInstance();
// Last week
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(MODIFIED);
date.add(Calendar.WEEK_OF_YEAR, -1);
rangeQueryBuilder.gt(date.getTimeInMillis()).boost(10);
booleanQuery.should(rangeQueryBuilder);
// Last month
rangeQueryBuilder = QueryBuilders.rangeQuery(MODIFIED);
date.add(Calendar.WEEK_OF_YEAR, -3);
rangeQueryBuilder.gt(date.getTimeInMillis()).boost(5);
booleanQuery.should(rangeQueryBuilder);
// Last year
rangeQueryBuilder = QueryBuilders.rangeQuery(MODIFIED);
date.add(Calendar.MONTH, -5);
rangeQueryBuilder.gt(date.getTimeInMillis()).boost(2);
booleanQuery.should(rangeQueryBuilder);
}
QueryBuilder unfilteredQuery = queryBuilder;
List<FilterBuilder> filters = new ArrayList<FilterBuilder>();