Search in sources :

Example 6 with FieldInstance

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

the class SolrInformationServer method getCascadeNodes.

public List<NodeMetaData> getCascadeNodes(List<Long> txnIds) throws AuthenticationException, IOException, JSONException {
    List<FieldInstance> list = AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(ContentModel.PROP_CASCADE_TX).getFields();
    FieldInstance fieldInstance = list.get(0);
    RefCounted<SolrIndexSearcher> refCounted = null;
    IntArrayList docList = null;
    HashSet<Long> childIds = new HashSet();
    try {
        refCounted = core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        String field = fieldInstance.getField();
        SchemaField schemaField = searcher.getSchema().getField(field);
        FieldType fieldType = schemaField.getType();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery booleanQuery = null;
        for (Long l : txnIds) {
            BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
            fieldType.readableToIndexed(l.toString(), bytesRefBuilder);
            TermQuery termQuery = new TermQuery(new Term(field, bytesRefBuilder.toBytesRef()));
            BooleanClause booleanClause = new BooleanClause(termQuery, BooleanClause.Occur.SHOULD);
            builder.add(booleanClause);
        }
        booleanQuery = builder.build();
        DocListCollector collector = new DocListCollector();
        searcher.search(booleanQuery, collector);
        docList = collector.getDocs();
        // System.out.println("################ CASCASDE Parent Nodes:"+docList.size());
        int size = docList.size();
        Set set = new HashSet();
        set.add(FIELD_SOLR4_ID);
        for (int i = 0; i < size; i++) {
            int docId = docList.get(i);
            Document document = searcher.doc(docId, set);
            IndexableField indexableField = document.getField(FIELD_SOLR4_ID);
            String id = indexableField.stringValue();
            TenantAclIdDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
            // System.out.println("################## Cascade Parent:"+ ids.dbId);
            childIds.add(ids.dbId);
        }
    } finally {
        refCounted.decref();
    }
    List<NodeMetaData> allNodeMetaDatas = new ArrayList();
    for (Long childId : childIds) {
        NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
        nmdp.setFromNodeId(childId);
        nmdp.setToNodeId(childId);
        nmdp.setIncludeAclId(false);
        nmdp.setIncludeAspects(false);
        nmdp.setIncludeChildAssociations(false);
        nmdp.setIncludeChildIds(true);
        nmdp.setIncludeNodeRef(false);
        nmdp.setIncludeOwner(false);
        nmdp.setIncludeParentAssociations(false);
        // We only care about the path and ancestors (which is included) for this case
        nmdp.setIncludePaths(true);
        nmdp.setIncludeProperties(false);
        nmdp.setIncludeType(false);
        nmdp.setIncludeTxnId(true);
        // Gets only one
        List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, 1);
        allNodeMetaDatas.addAll(nodeMetaDatas);
    }
    return allNodeMetaDatas;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Set(java.util.Set) AclChangeSet(org.alfresco.solr.client.AclChangeSet) LinkedHashSet(java.util.LinkedHashSet) IOpenBitSet(org.alfresco.solr.adapters.IOpenBitSet) HashSet(java.util.HashSet) TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) IntArrayList(com.carrotsearch.hppc.IntArrayList) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) TermQuery(org.apache.lucene.search.TermQuery) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) NodeMetaData(org.alfresco.solr.client.NodeMetaData) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Term(org.apache.lucene.index.Term) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) BooleanClause(org.apache.lucene.search.BooleanClause) IndexableField(org.apache.lucene.index.IndexableField) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 7 with FieldInstance

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

the class SolrInformationServer method addPropertiesToDoc.

static void addPropertiesToDoc(Map<QName, PropertyValue> properties, boolean isContentIndexedForNode, SolrInputDocument newDoc, SolrInputDocument cachedDoc, boolean transformContentFlag) throws IOException {
    for (QName propertyQName : properties.keySet()) {
        newDoc.addField(FIELD_PROPERTIES, propertyQName.toString());
        newDoc.addField(FIELD_PROPERTIES, propertyQName.getPrefixString());
        PropertyValue value = properties.get(propertyQName);
        // System.out.println("####### indexing prop:"+propertyQName.toString()+":"+value.toString());
        if (value != null) {
            if (value instanceof StringPropertyValue) {
                for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
                    // System.out.println("####### field:"+field.getField()+":"+value.toString());
                    addStringPropertyToDoc(newDoc, field, (StringPropertyValue) value, properties);
                }
            } else if (value instanceof MLTextPropertyValue) {
                for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
                    addMLTextPropertyToDoc(newDoc, field, (MLTextPropertyValue) value);
                }
            } else if (value instanceof ContentPropertyValue) {
                boolean transform = transformContentFlag;
                if (!isContentIndexedForNode) {
                    transform = false;
                }
                addContentPropertyToDocUsingCache(newDoc, cachedDoc, propertyQName, (ContentPropertyValue) value, transform);
            } else if (value instanceof MultiPropertyValue) {
                MultiPropertyValue typedValue = (MultiPropertyValue) value;
                for (PropertyValue singleValue : typedValue.getValues()) {
                    if (singleValue instanceof StringPropertyValue) {
                        for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
                            addStringPropertyToDoc(newDoc, field, (StringPropertyValue) singleValue, properties);
                        }
                    } else if (singleValue instanceof MLTextPropertyValue) {
                        for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
                            addMLTextPropertyToDoc(newDoc, field, (MLTextPropertyValue) singleValue);
                        }
                    } else if (singleValue instanceof ContentPropertyValue) {
                        boolean transform = transformContentFlag;
                        if (!isContentIndexedForNode) {
                            transform = false;
                        }
                        addContentPropertyToDocUsingCache(newDoc, cachedDoc, propertyQName, (ContentPropertyValue) singleValue, transform);
                    }
                }
            }
        } else {
            // NULL property
            newDoc.addField(FIELD_NULLPROPERTIES, propertyQName.toString());
        }
    }
}
Also used : StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) MultiPropertyValue(org.alfresco.solr.client.MultiPropertyValue) QName(org.alfresco.service.namespace.QName) MLTextPropertyValue(org.alfresco.solr.client.MLTextPropertyValue) StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) MLTextPropertyValue(org.alfresco.solr.client.MLTextPropertyValue) ContentPropertyValue(org.alfresco.solr.client.ContentPropertyValue) MultiPropertyValue(org.alfresco.solr.client.MultiPropertyValue) PropertyValue(org.alfresco.solr.client.PropertyValue) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) ContentPropertyValue(org.alfresco.solr.client.ContentPropertyValue)

Example 8 with FieldInstance

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

the class SolrInformationServer method addContentPropertyToDocUsingAlfrescoRepository.

private void addContentPropertyToDocUsingAlfrescoRepository(SolrInputDocument doc, QName propertyQName, long dbId, String locale) throws AuthenticationException, IOException, UnsupportedEncodingException {
    long start = System.nanoTime();
    // Expensive call to be done with ContentTracker
    GetTextContentResponse response = repositoryClient.getTextContent(dbId, propertyQName, null);
    addContentPropertyMetadata(doc, propertyQName, AlfrescoSolrDataModel.ContentFieldType.TRANSFORMATION_STATUS, response);
    addContentPropertyMetadata(doc, propertyQName, AlfrescoSolrDataModel.ContentFieldType.TRANSFORMATION_EXCEPTION, response);
    addContentPropertyMetadata(doc, propertyQName, AlfrescoSolrDataModel.ContentFieldType.TRANSFORMATION_TIME, response);
    InputStream ris = response.getContent();
    String textContent = "";
    try {
        if (ris != null) {
            // Get and copy content
            byte[] bytes = FileCopyUtils.copyToByteArray(new BoundedInputStream(ris, contentStreamLimit));
            textContent = new String(bytes, "UTF8");
        }
    } finally {
        // release the response only when the content has been read
        response.release();
    }
    if (minHash && textContent.length() > 0) {
        Analyzer analyzer = core.getLatestSchema().getFieldType("min_hash").getIndexAnalyzer();
        TokenStream ts = analyzer.tokenStream("min_hash", textContent);
        CharTermAttribute termAttribute = ts.getAttribute(CharTermAttribute.class);
        ts.reset();
        while (ts.incrementToken()) {
            StringBuilder tokenBuff = new StringBuilder();
            char[] buff = termAttribute.buffer();
            for (int i = 0; i < termAttribute.length(); i++) {
                tokenBuff.append(Integer.toHexString(buff[i]));
            }
            doc.addField(FINGERPRINT_FIELD, tokenBuff.toString());
        }
        ts.end();
        ts.close();
    }
    long end = System.nanoTime();
    this.getTrackerStats().addDocTransformationTime(end - start);
    StringBuilder builder = new StringBuilder(textContent.length() + 16);
    builder.append("\u0000").append(locale).append("\u0000");
    builder.append(textContent);
    String localisedText = builder.toString();
    for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
        doc.removeField(field.getField());
        if (field.isLocalised()) {
            doc.addField(field.getField(), localisedText);
        } else {
            doc.addField(field.getField(), textContent);
        }
        addFieldIfNotSet(doc, field);
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) GetTextContentResponse(org.alfresco.solr.client.SOLRAPIClient.GetTextContentResponse) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) Analyzer(org.apache.lucene.analysis.Analyzer) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance)

Example 9 with FieldInstance

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

the class SolrInformationServer method getSolrFieldNameForContentPropertyMetadata.

/**
 * Gets the field name used in Solr for the specified content property.
 * Assumes that the first defined field in Solr is the "right one".
 * @param propertyQName the content property qualified name
 * @param type the content property field type, i.e. DOCID
 * @return a String representing the name of the field in Solr or null if not found
 */
private static String getSolrFieldNameForContentPropertyMetadata(QName propertyQName, AlfrescoSolrDataModel.ContentFieldType type) {
    IndexedField indexedField = AlfrescoSolrDataModel.getInstance().getIndexedFieldForContentPropertyMetadata(propertyQName, type);
    List<FieldInstance> fields = indexedField.getFields();
    String fieldName = null;
    if (fields != null && !fields.isEmpty()) {
        FieldInstance instance = fields.get(0);
        if (instance != null) {
            fieldName = instance.getField();
        }
    }
    return fieldName;
}
Also used : IndexedField(org.alfresco.solr.AlfrescoSolrDataModel.IndexedField) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance)

Example 10 with FieldInstance

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

the class SolrInformationServer method indexNonShardCascade.

private void indexNonShardCascade(NodeMetaData nodeMetaData) throws IOException {
    canUpdate();
    SolrQueryRequest request = null;
    UpdateRequestProcessor processor = null;
    try {
        request = getLocalSolrQueryRequest();
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        StringPropertyValue stringPropertyValue = (StringPropertyValue) nodeMetaData.getProperties().get(ContentModel.PROP_CASCADE_TX);
        List<FieldInstance> fieldInstances = AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(ContentModel.PROP_CASCADE_TX).getFields();
        FieldInstance fieldInstance = fieldInstances.get(0);
        AddUpdateCommand cmd = new AddUpdateCommand(request);
        SolrInputDocument input = new SolrInputDocument();
        input.addField(FIELD_SOLR4_ID, AlfrescoSolrDataModel.getNodeDocumentId(nodeMetaData.getTenantDomain(), nodeMetaData.getAclId(), nodeMetaData.getId()));
        input.addField(FIELD_VERSION, 0);
        input.addField(fieldInstance.getField(), stringPropertyValue.toString());
        cmd.solrDoc = input;
        processor.processAdd(cmd);
    } finally {
        if (processor != null) {
            processor.finish();
        }
        if (request != null) {
            request.close();
        }
    }
}
Also used : StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

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