Search in sources :

Example 1 with SortOrder

use of org.elasticsearch.search.sort.SortOrder in project fess by codelibs.

the class QueryHelper method convertTermQuery.

protected QueryBuilder convertTermQuery(final QueryContext context, final TermQuery termQuery, final float boost) {
    final String field = termQuery.getTerm().field();
    final String text = termQuery.getTerm().text();
    if (fessConfig.getQueryReplaceTermWithPrefixQueryAsBoolean() && text.length() > 1 && text.endsWith("*")) {
        return convertPrefixQuery(context, new PrefixQuery(new Term(field, text.substring(0, text.length() - 1))), boost);
    } else if (Constants.DEFAULT_FIELD.equals(field)) {
        context.addFieldLog(field, text);
        context.addHighlightedQuery(text);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, text).boost(b * boost));
    } else if ("sort".equals(field)) {
        final String[] values = text.split("\\.");
        if (values.length > 2) {
            throw new InvalidQueryException(messages -> messages.addErrorsInvalidQuerySortValue(UserMessages.GLOBAL_PROPERTY_KEY, text), "Invalid sort field: " + termQuery);
        }
        final String sortField = values[0];
        if (!isSortField(sortField)) {
            throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnsupportedSortField(UserMessages.GLOBAL_PROPERTY_KEY, sortField), "Unsupported sort field: " + termQuery);
        }
        SortOrder sortOrder;
        if (values.length == 2) {
            sortOrder = SortOrder.DESC.toString().equalsIgnoreCase(values[1]) ? SortOrder.DESC : SortOrder.ASC;
            if (sortOrder == null) {
                throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnsupportedSortOrder(UserMessages.GLOBAL_PROPERTY_KEY, values[1]), "Invalid sort order: " + termQuery);
            }
        } else {
            sortOrder = SortOrder.ASC;
        }
        context.addSorts(createFieldSortBuilder(sortField, sortOrder));
        return null;
    } else if (INURL_FIELD.equals(field)) {
        return QueryBuilders.wildcardQuery(fessConfig.getIndexFieldUrl(), "*" + text + "*").boost(boost);
    } else if (isSearchField(field)) {
        context.addFieldLog(field, text);
        context.addHighlightedQuery(text);
        if (notAnalyzedFieldSet.contains(field)) {
            return QueryBuilders.termQuery(field, text).boost(boost);
        } else {
            return QueryBuilders.matchPhraseQuery(field, text).boost(boost);
        }
    } else {
        final String origQuery = termQuery.toString();
        context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
        context.addHighlightedQuery(origQuery);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, origQuery).boost(b * boost));
    }
}
Also used : Query(org.apache.lucene.search.Query) Constants(org.codelibs.fess.Constants) SortBuilders(org.elasticsearch.search.sort.SortBuilders) OptionalThing(org.dbflute.optional.OptionalThing) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ScoreFunctionBuilders(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders) GeoInfo(org.codelibs.fess.entity.GeoInfo) QueryContext(org.codelibs.fess.entity.QueryContext) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Fuzziness(org.elasticsearch.common.unit.Fuzziness) Locale(java.util.Locale) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Resource(javax.annotation.Resource) Set(java.util.Set) PrefixQuery(org.apache.lucene.search.PrefixQuery) UUID(java.util.UUID) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) WildcardQuery(org.apache.lucene.search.WildcardQuery) List(java.util.List) Stream(java.util.stream.Stream) ComponentUtil(org.codelibs.fess.util.ComponentUtil) PostConstruct(javax.annotation.PostConstruct) SortOrder(org.elasticsearch.search.sort.SortOrder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ParseException(org.apache.lucene.queryparser.classic.ParseException) HashMap(java.util.HashMap) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FacetInfo(org.codelibs.fess.entity.FacetInfo) SortBuilder(org.elasticsearch.search.sort.SortBuilder) UserMessages(org.lastaflute.core.message.UserMessages) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) StringUtil(org.codelibs.core.lang.StringUtil) BooleanClause(org.apache.lucene.search.BooleanClause) Consumer(java.util.function.Consumer) TermQuery(org.apache.lucene.search.TermQuery) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) PrefixQuery(org.apache.lucene.search.PrefixQuery) SortOrder(org.elasticsearch.search.sort.SortOrder) Term(org.apache.lucene.index.Term) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Consumer (java.util.function.Consumer)1 Stream (java.util.stream.Stream)1 PostConstruct (javax.annotation.PostConstruct)1 Resource (javax.annotation.Resource)1 Term (org.apache.lucene.index.Term)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1 BooleanClause (org.apache.lucene.search.BooleanClause)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1