Search in sources :

Example 11 with FieldInstance

use of org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance in project SearchServices by Alfresco.

the class Solr4QueryParser method addLocaleSpecificTextRange.

private void addLocaleSpecificTextRange(String expandedFieldName, PropertyDefinition pDef, String part1, String part2, boolean includeLower, boolean includeUpper, Builder booleanQuery, Locale locale, AnalysisMode analysisMode, IndexTokenisationMode tokenisationMode, IndexTokenisationMode preferredTokenisationMode) throws ParseException, IOException {
    FieldInstance fieldInstance = getFieldInstance(expandedFieldName, pDef, locale, preferredTokenisationMode);
    String firstString = null;
    if ((part1 != null) && !part1.equals("\u0000")) {
        if (fieldInstance.isLocalised()) {
            firstString = getFirstTokenForRange(getLocalePrefixedText(part1, locale), fieldInstance);
            if (firstString == null) {
                firstString = "{" + locale.getLanguage() + "}";
            }
        } else {
            firstString = getFirstTokenForRange(part1, fieldInstance);
        }
    } else {
        if (fieldInstance.isLocalised()) {
            firstString = "{" + locale.getLanguage() + "}";
        } else {
            firstString = null;
        }
    }
    String lastString = null;
    if ((part2 != null) && !part2.equals("\uffff")) {
        if (fieldInstance.isLocalised()) {
            lastString = getFirstTokenForRange(getLocalePrefixedText(part2, locale), fieldInstance);
            if (lastString == null) {
                lastString = "{" + locale.getLanguage() + "}\uffff";
            }
        } else {
            lastString = getFirstTokenForRange(part2, fieldInstance);
        }
    } else {
        if (fieldInstance.isLocalised()) {
            lastString = "{" + locale.getLanguage() + "}\uffff";
        } else {
            lastString = null;
        }
    }
    TermRangeQuery query = new TermRangeQuery(fieldInstance.getField(), firstString == null ? null : new BytesRef(firstString), lastString == null ? null : new BytesRef(lastString), includeLower, includeUpper);
    booleanQuery.add(query, Occur.SHOULD);
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) BytesRef(org.apache.lucene.util.BytesRef)

Example 12 with FieldInstance

use of org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance in project SearchServices by Alfresco.

the class Solr4QueryParser method attributeQueryBuilder.

private Query attributeQueryBuilder(String field, String queryText, SubQuery subQueryBuilder, AnalysisMode analysisMode, LuceneFunction luceneFunction) throws ParseException {
    // TODO: Fix duplicate token generation for mltext, content and text.
    // -locale expansion here and in tokeisation -> duplicates
    // Get type info etc
    // TODO: additional suffixes
    Pair<String, String> fieldNameAndEnding = QueryParserUtils.extractFieldNameAndEnding(field);
    String expandedFieldName = null;
    QName propertyQName;
    PropertyDefinition propertyDef = QueryParserUtils.matchPropertyDefinition(searchParameters.getNamespace(), namespacePrefixResolver, dictionaryService, fieldNameAndEnding.getFirst());
    IndexTokenisationMode tokenisationMode = IndexTokenisationMode.TRUE;
    if (propertyDef != null) {
        tokenisationMode = propertyDef.getIndexTokenisationMode();
        if (tokenisationMode == null) {
            tokenisationMode = IndexTokenisationMode.TRUE;
        }
        propertyQName = propertyDef.getName();
    } else {
        expandedFieldName = expandAttributeFieldName(field);
        propertyQName = QName.createQName(fieldNameAndEnding.getFirst());
    }
    if (isAllStar(queryText)) {
        return createTermQuery(FIELD_PROPERTIES, propertyQName.toString());
    }
    if (luceneFunction != LuceneFunction.FIELD) {
        if ((tokenisationMode == IndexTokenisationMode.FALSE) || (tokenisationMode == IndexTokenisationMode.BOTH)) {
            if (luceneFunction == LuceneFunction.LOWER) {
                if (false == queryText.toLowerCase().equals(queryText)) {
                    return createNoMatchQuery();
                }
            }
            if (luceneFunction == LuceneFunction.UPPER) {
                if (false == queryText.toUpperCase().equals(queryText)) {
                    return createNoMatchQuery();
                }
            }
            return functionQueryBuilder(expandedFieldName, fieldNameAndEnding.getSecond(), propertyQName, propertyDef, tokenisationMode, queryText, luceneFunction);
        }
    }
    // Mime type
    if (fieldNameAndEnding.getSecond().equals(FIELD_MIMETYPE_SUFFIX)) {
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
            return subQueryBuilder.getQuery(AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyQName, ContentFieldType.MIMETYPE, FieldUse.ID).getFields().get(0).getField(), queryText, analysisMode, luceneFunction);
        }
    } else if (fieldNameAndEnding.getSecond().equals(FIELD_SIZE_SUFFIX)) {
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
            return subQueryBuilder.getQuery(AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyQName, ContentFieldType.SIZE, FieldUse.ID).getFields().get(0).getField(), queryText, analysisMode, luceneFunction);
        }
    } else if (fieldNameAndEnding.getSecond().equals(FIELD_LOCALE_SUFFIX)) {
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
            return subQueryBuilder.getQuery(AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyQName, ContentFieldType.LOCALE, FieldUse.ID).getFields().get(0).getField(), queryText, analysisMode, luceneFunction);
        }
    } else if (fieldNameAndEnding.getSecond().equals(FIELD_ENCODING_SUFFIX)) {
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
            return subQueryBuilder.getQuery(AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyQName, ContentFieldType.ENCODING, FieldUse.ID).getFields().get(0).getField(), queryText, analysisMode, luceneFunction);
        }
    } else if (fieldNameAndEnding.getSecond().equals(FIELD_TRANSFORMATION_STATUS_SUFFIX)) {
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
            return subQueryBuilder.getQuery(AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyQName, ContentFieldType.TRANSFORMATION_STATUS, FieldUse.ID).getFields().get(0).getField(), queryText, analysisMode, luceneFunction);
        }
    } else if (fieldNameAndEnding.getSecond().equals(FIELD_TRANSFORMATION_TIME_SUFFIX)) {
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
            return subQueryBuilder.getQuery(AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyQName, ContentFieldType.TRANSFORMATION_TIME, FieldUse.ID).getFields().get(0).getField(), queryText, analysisMode, luceneFunction);
        }
    } else if (fieldNameAndEnding.getSecond().equals(FIELD_TRANSFORMATION_EXCEPTION_SUFFIX)) {
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
            return subQueryBuilder.getQuery(AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyQName, ContentFieldType.TRANSFORMATION_EXCEPTION, FieldUse.ID).getFields().get(0).getField(), queryText, analysisMode, luceneFunction);
        }
    }
    if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.MLTEXT))) {
        // Build a sub query for each locale and or the results together -
        // the analysis will take care of
        // cross language matching for each entry
        BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
        List<Locale> locales = searchParameters.getLocales();
        List<Locale> expandedLocales = new ArrayList<Locale>();
        for (Locale locale : (((locales == null) || (locales.size() == 0)) ? Collections.singletonList(I18NUtil.getLocale()) : locales)) {
            expandedLocales.addAll(MLAnalysisMode.getLocales(mlAnalysisMode, locale, false));
        }
        for (Locale locale : (((expandedLocales == null) || (expandedLocales.size() == 0)) ? Collections.singletonList(I18NUtil.getLocale()) : expandedLocales)) {
            addMLTextAttributeQuery(field, propertyDef, queryText, subQueryBuilder, analysisMode, luceneFunction, expandedFieldName, propertyDef, tokenisationMode, booleanQuery, locale);
        }
        return getNonEmptyBooleanQuery(booleanQuery.build());
    } else // Content
    if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.CONTENT))) {
        // Identifier request are ignored for content
        // Build a sub query for each locale and or the results together -
        // - add an explicit condition for the locale
        List<Locale> locales = searchParameters.getLocales();
        List<Locale> expandedLocales = new ArrayList<Locale>();
        for (Locale locale : (((locales == null) || (locales.size() == 0)) ? Collections.singletonList(I18NUtil.getLocale()) : locales)) {
            expandedLocales.addAll(MLAnalysisMode.getLocales(mlAnalysisMode, locale, addContentCrossLocaleWildcards()));
        }
        return addContentAttributeQuery(propertyDef, queryText, subQueryBuilder, analysisMode, luceneFunction, expandedFieldName, expandedLocales);
    } else if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.TEXT))) {
        // if (propertyQName.equals(ContentModel.PROP_USER_USERNAME) ||
        // propertyQName.equals(ContentModel.PROP_USERNAME) ||
        // propertyQName.equals(ContentModel.PROP_AUTHORITY_NAME))
        // {
        // // nasty work around for solr support for user and group look up
        // as we can not support lowercased identifiers in the model
        // if(isLucene())
        // {
        // return subQueryBuilder.getQuery(expandedFieldName, queryText,
        // analysisMode, luceneFunction);
        // }
        // }
        boolean withWildCards = propertyQName.equals(ContentModel.PROP_USER_USERNAME) || propertyQName.equals(ContentModel.PROP_USERNAME) || propertyQName.equals(ContentModel.PROP_AUTHORITY_NAME);
        BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
        List<Locale> locales = searchParameters.getLocales();
        List<Locale> expandedLocales = new ArrayList<Locale>();
        for (Locale locale : (((locales == null) || (locales.size() == 0)) ? Collections.singletonList(I18NUtil.getLocale()) : locales)) {
            expandedLocales.addAll(MLAnalysisMode.getLocales(mlAnalysisMode, locale, withWildCards));
        }
        for (Locale locale : (((expandedLocales == null) || (expandedLocales.size() == 0)) ? Collections.singletonList(I18NUtil.getLocale()) : expandedLocales)) {
            Locale fixedLocale = locale;
            if (fixedLocale.getLanguage().equals("*")) {
                fixedLocale = new Locale("??");
            }
            addTextAttributeQuery(field, propertyDef, queryText, subQueryBuilder, analysisMode, luceneFunction, expandedFieldName, tokenisationMode, booleanQuery, fixedLocale);
        }
        return getNonEmptyBooleanQuery(booleanQuery.build());
    } else {
        // Date does not support like
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME))) {
            if (analysisMode == AnalysisMode.LIKE) {
                throw new UnsupportedOperationException("Wild cards are not supported for the datetime type");
            }
        }
        // expand date for loose date parsing
        if ((propertyDef != null) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME) || propertyDef.getDataType().getName().equals(DataTypeDefinition.DATE))) {
            Pair<Date, Integer> dateAndResolution = parseDateString(queryText);
            BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
            IndexedField indexedField = AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyDef.getName(), null, FieldUse.FTS);
            for (FieldInstance instance : indexedField.getFields()) {
                if (dateAndResolution != null) {
                    Query query = newRangeQuery(instance.getField(), getDateStart(dateAndResolution), getDateEnd(dateAndResolution), true, true);
                    if (query != null) {
                        bQuery.add(query, Occur.SHOULD);
                    }
                } else {
                    Query query = subQueryBuilder.getQuery(instance.getField(), queryText, AnalysisMode.DEFAULT, luceneFunction);
                    if (query != null) {
                        bQuery.add(query, Occur.SHOULD);
                    }
                }
            }
            if (bQuery.build().clauses().size() > 0) {
                return bQuery.build();
            } else {
                return createNoMatchQuery();
            }
        }
        if ((propertyDef != null) && (tenantService.isTenantUser()) && (propertyDef.getDataType().getName().equals(DataTypeDefinition.NODE_REF)) && (queryText.contains(StoreRef.URI_FILLER))) {
            // ALF-6202
            queryText = tenantService.getName(new NodeRef(queryText)).toString();
        }
        // Dates are not special in this case
        if (propertyDef != null) {
            BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
            IndexedField indexedField = AlfrescoSolrDataModel.getInstance().getQueryableFields(propertyDef.getName(), null, FieldUse.FTS);
            for (FieldInstance instance : indexedField.getFields()) {
                Query query = subQueryBuilder.getQuery(instance.getField(), queryText, AnalysisMode.DEFAULT, luceneFunction);
                if (query != null) {
                    bQuery.add(query, Occur.SHOULD);
                }
            }
            if (bQuery.build().clauses().size() > 0) {
                return bQuery.build();
            } else {
                return createNoMatchQuery();
            }
        } else {
            Query query = subQueryBuilder.getQuery(expandedFieldName, queryText, AnalysisMode.DEFAULT, luceneFunction);
            if (query != null) {
                return query;
            } else {
                return createNoMatchQuery();
            }
        }
    }
}
Also used : Locale(java.util.Locale) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) QName(org.alfresco.service.namespace.QName) IndexedField(org.alfresco.solr.AlfrescoSolrDataModel.IndexedField) Builder(org.apache.lucene.search.BooleanQuery.Builder) ArrayList(java.util.ArrayList) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) IndexTokenisationMode(org.alfresco.repo.dictionary.IndexTokenisationMode) Builder(org.apache.lucene.search.BooleanQuery.Builder) Date(java.util.Date) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) LinkedList(java.util.LinkedList) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance)

Example 13 with FieldInstance

use of org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance in project SearchServices by Alfresco.

the class Solr4QueryParser method addContentAttributeQuery.

// TODO: correct field names
protected Query addContentAttributeQuery(PropertyDefinition pDef, String queryText, SubQuery subQueryBuilder, AnalysisMode analysisMode, LuceneFunction luceneFunction, String expandedFieldName, List<Locale> expandedLocales) throws ParseException {
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    for (Locale locale : expandedLocales) {
        if (locale.toString().length() == 0) {
            IndexedField indexedField = AlfrescoSolrDataModel.getInstance().getQueryableFields(pDef.getName(), null, FieldUse.FTS);
            for (FieldInstance field : indexedField.getFields()) {
                if (!field.isLocalised()) {
                    Query subQuery = subQueryBuilder.getQuery(field.getField(), queryText, analysisMode, luceneFunction);
                    if (subQuery != null) {
                        booleanQuery.add(subQuery, Occur.SHOULD);
                    }
                }
            }
        } else {
            StringBuilder builder = new StringBuilder(queryText.length() + 10);
            builder.append("\u0000").append(locale.toString()).append("\u0000").append(queryText);
            IndexedField indexedField = AlfrescoSolrDataModel.getInstance().getQueryableFields(pDef.getName(), null, FieldUse.FTS);
            for (FieldInstance field : indexedField.getFields()) {
                if (field.isLocalised()) {
                    Query subQuery = subQueryBuilder.getQuery(field.getField(), builder.toString(), analysisMode, luceneFunction);
                    if (subQuery != null) {
                        booleanQuery.add(subQuery, Occur.SHOULD);
                    }
                }
            }
        }
    }
    return getNonEmptyBooleanQuery(booleanQuery.build());
}
Also used : Locale(java.util.Locale) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexedField(org.alfresco.solr.AlfrescoSolrDataModel.IndexedField) Builder(org.apache.lucene.search.BooleanQuery.Builder) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) Builder(org.apache.lucene.search.BooleanQuery.Builder)

Aggregations

FieldInstance (org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance)13 IndexedField (org.alfresco.solr.AlfrescoSolrDataModel.IndexedField)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)6 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)6 SpanQuery (org.apache.lucene.search.spans.SpanQuery)6 TermQuery (org.apache.lucene.search.TermQuery)5 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)5 Locale (java.util.Locale)4 Builder (org.apache.lucene.search.BooleanQuery.Builder)4 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)4 Query (org.apache.lucene.search.Query)4 RegexpQuery (org.apache.lucene.search.RegexpQuery)4 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 QName (org.alfresco.service.namespace.QName)3 HashSet (java.util.HashSet)2