Search in sources :

Example 1 with QueryContext

use of org.codelibs.fess.entity.QueryContext in project fess by codelibs.

the class SearchService method deleteByQuery.

public int deleteByQuery(final HttpServletRequest request, final SearchRequestParams params) {
    final String query = QueryStringBuilder.query(params.getQuery()).extraQueries(params.getExtraQueries()).fields(params.getFields()).build();
    final QueryContext queryContext = queryHelper.build(params.getType(), query, context -> {
        context.skipRoleQuery();
    });
    return fessEsClient.deleteByQuery(fessConfig.getIndexDocumentUpdateIndex(), fessConfig.getIndexDocumentType(), queryContext.getQueryBuilder());
}
Also used : QueryContext(org.codelibs.fess.entity.QueryContext)

Example 2 with QueryContext

use of org.codelibs.fess.entity.QueryContext in project fess by codelibs.

the class QueryHelper method convertFuzzyQuery.

protected QueryBuilder convertFuzzyQuery(final QueryContext context, final FuzzyQuery fuzzyQuery, final float boost) {
    final Term term = fuzzyQuery.getTerm();
    final String field = term.field();
    // TODO fuzzy value
    if (Constants.DEFAULT_FIELD.equals(field)) {
        context.addFieldLog(field, term.text());
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, term.text()).fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b * boost));
    } else if (isSearchField(field)) {
        context.addFieldLog(field, term.text());
        return QueryBuilders.fuzzyQuery(field, term.text()).boost(boost).fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits()));
    } else {
        final String origQuery = fuzzyQuery.toString();
        context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
        context.addHighlightedQuery(origQuery);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, origQuery).fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).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) Term(org.apache.lucene.index.Term)

Example 3 with QueryContext

use of org.codelibs.fess.entity.QueryContext in project fess by codelibs.

the class QueryHelper method build.

public QueryContext build(final SearchRequestType searchRequestType, final String query, final Consumer<QueryContext> context) {
    String q;
    if (additionalQuery != null && StringUtil.isNotBlank(query)) {
        q = query + " " + additionalQuery;
    } else {
        q = query;
    }
    final QueryContext queryContext = new QueryContext(q, true);
    buildBaseQuery(queryContext, context);
    buildBoostQuery(queryContext);
    buildRoleQuery(queryContext, searchRequestType);
    if (!queryContext.hasSorts() && defaultSortBuilders != null) {
        queryContext.addSorts(defaultSortBuilders);
    }
    return queryContext;
}
Also used : QueryContext(org.codelibs.fess.entity.QueryContext)

Example 4 with QueryContext

use of org.codelibs.fess.entity.QueryContext 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)

Example 5 with QueryContext

use of org.codelibs.fess.entity.QueryContext in project fess by codelibs.

the class QueryHelper method buildBaseQuery.

public void buildBaseQuery(final QueryContext queryContext, final Consumer<QueryContext> context) {
    try {
        final Query query = getQueryParser().parse(queryContext.getQueryString());
        final QueryBuilder queryBuilder = convertQuery(queryContext, query, 1.0f);
        if (queryBuilder != null) {
            queryContext.setQueryBuilder(queryBuilder);
        } else {
            queryContext.setQueryBuilder(QueryBuilders.matchAllQuery());
        }
        // TODO options query
        context.accept(queryContext);
    } catch (final ParseException e) {
        throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryParseError(UserMessages.GLOBAL_PROPERTY_KEY), "Invalid query: " + queryContext.getQueryString(), e);
    }
}
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) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) ParseException(org.apache.lucene.queryparser.classic.ParseException) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException)

Aggregations

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