Example usage for org.apache.solr.search DocSet size

List of usage examples for org.apache.solr.search DocSet size

Introduction

In this page you can find the example usage for org.apache.solr.search DocSet size.

Prototype

public int size();

Source Link

Document

Returns the number of documents in the set.

Usage

From source file:com.doculibre.constellio.solr.handler.component.ConstellioAuthorizationComponent.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  w  w.j  av  a  2 s .co m
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrIndexSearcher searcher = req.getSearcher();
    //IndexReader reader = req.getSearcher().getReader();
    SolrParams params = req.getParams();

    // A runtime param can skip
    if (!params.getBool(ENABLE, true)) {
        return;
    }

    Query query = rb.getQuery();
    String qstr = rb.getQueryString();
    if (query == null || qstr == null) {
        return;
    }

    ConstellioUser user;
    String userIdStr = params.get(ConstellioSolrQueryParams.USER_ID);
    if (userIdStr != null) {
        UserServices userServices = ConstellioSpringUtils.getUserServices();
        try {
            user = userServices.get(new Long(userIdStr));
        } catch (NumberFormatException e) {
            user = null;
        }
    } else {
        user = null;
    }

    String collectionName = params.get(ConstellioSolrQueryParams.COLLECTION_NAME);
    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    FederationServices federationServices = ConstellioSpringUtils.getFederationServices();
    RecordCollection collection = collectionServices.get(collectionName);

    List<TermQuery> restrictedCollectionQueries = new ArrayList<TermQuery>();
    if (collection.isFederationOwner()) {
        List<RecordCollection> includedCollections = federationServices.listIncludedCollections(collection);
        for (RecordCollection includedCollection : includedCollections) {
            if (includedCollection.hasSearchPermission()
                    && (user == null || !user.hasSearchPermission(includedCollection))) {
                restrictedCollectionQueries.add(new TermQuery(
                        new Term(IndexField.COLLECTION_ID_FIELD, "" + includedCollection.getId())));
            }
        }
    }

    // User must be logged in to see private records
    if (user != null) {
        String luceneQueryStr = params.get(ConstellioSolrQueryParams.LUCENE_QUERY);
        if (StringUtils.isBlank(luceneQueryStr)) {
            return;
        }

        IndexSchema schema = req.getSchema();
        SolrQueryParser queryParser = new SolrQueryParser(rb.getQparser(), IndexField.DEFAULT_SEARCH_FIELD);
        Query luceneQuery;
        try {
            luceneQuery = queryParser.parse(luceneQueryStr);
        } catch (SyntaxError e) {
            log.error("Error parsing lucene query " + luceneQueryStr, e);
            return;
        }
        // Create a new query which will only include private records
        BooleanQuery privateRecordQuery = new BooleanQuery(true);
        privateRecordQuery.add(luceneQuery, BooleanClause.Occur.MUST);
        for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) {
            privateRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT);
        }

        TermQuery privateRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "F"));
        privateRecordQuery.add(privateRecordTQ, BooleanClause.Occur.MUST);

        DocSet privateRecordIdDocSet = searcher.getDocSet(privateRecordQuery);

        if (privateRecordIdDocSet.size() > 0) {
            RecordServices recordServices = ConstellioSpringUtils.getRecordServices();
            ACLServices aclServices = ConstellioSpringUtils.getACLServices();
            ConnectorManagerServices connectorManagerServices = ConstellioSpringUtils
                    .getConnectorManagerServices();

            List<Record> privateRecords = new ArrayList<Record>();
            DocIterator docIt = privateRecordIdDocSet.iterator();
            while (docIt.hasNext()) {
                int docId = docIt.nextDoc();
                Document luceneDoc = searcher.doc(docId);
                Long recordId = new Long(luceneDoc.get(IndexField.RECORD_ID_FIELD));
                Record record = recordServices.get(recordId, collection);
                privateRecords.add(record);
            }
            // First pass : Remove ACL authorized records
            List<Record> unevaluatedPrivateRecords = aclServices.removeAuthorizedRecords(privateRecords, user);
            if (!unevaluatedPrivateRecords.isEmpty()) {
                Set<UserCredentials> userCredentials = user.getUserCredentials();
                // Second pass : Ask the connector manager
                ConnectorManager connectorManager = connectorManagerServices.getDefaultConnectorManager();
                List<Record> authorizedRecords = connectorManagerServices
                        .authorizeByConnector(unevaluatedPrivateRecords, userCredentials, connectorManager);
                List<Record> unauthorizedRecords = ListUtils.removeAll(unevaluatedPrivateRecords,
                        authorizedRecords);

                if (!unauthorizedRecords.isEmpty()) {
                    // Create a new query which will exclude unauthorized records
                    BooleanQuery authorizedRecordQuery = new BooleanQuery(true);
                    authorizedRecordQuery.add(query, BooleanClause.Occur.MUST);
                    for (Record unauthorizedRecord : unauthorizedRecords) {
                        TermQuery unauthorizedRecordTQ = new TermQuery(
                                new Term(IndexField.RECORD_ID_FIELD, "" + unauthorizedRecord.getId()));
                        authorizedRecordQuery.add(unauthorizedRecordTQ, BooleanClause.Occur.MUST_NOT);
                    }
                    rb.setQuery(authorizedRecordQuery);
                }
            }
        }
    } else {
        BooleanQuery publicRecordQuery = new BooleanQuery(true);
        publicRecordQuery.add(query, BooleanClause.Occur.MUST);
        TermQuery publicRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "T"));
        publicRecordQuery.add(publicRecordTQ, BooleanClause.Occur.MUST);
        for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) {
            publicRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT);
        }
        rb.setQuery(publicRecordQuery);
    }
}

From source file:com.searchbox.SuggesterTreeHolder.java

License:Apache License

SuggestionResultSet getSuggestions(SolrIndexSearcher searcher, String[] fields, String query,
        int maxPhraseSearch) {
    query = deAccent(query);/*ww w . j a  v a  2s  . c o  m*/
    String[] queryTokens = query.replaceAll("[^A-Za-z0-9 ]", " ").replace("  ", " ").trim().split(" "); // TODO should use
    // tokensizer..
    SuggestionResultSet rs = headNode.computeQt(queryTokens[queryTokens.length - 1].toLowerCase(),
            maxPhraseSearch); // get completion for the first word in the
                                                                                                                             // suggestion

    // didn't find it, bail early
    if (rs == null) {
        return new SuggestionResultSet("", 0);
    }

    rs.myval = "";
    LOGGER.debug("Doing 2nd part of equation");
    try {

        if (queryTokens.length > 1) {

            // Solr 4.4 method change
            QueryParser parser = new QueryParser(Version.LUCENE_44, "contents",
                    searcher.getCore().getLatestSchema().getAnalyzer());
            // QueryParser parser = new QueryParser(Version.LUCENE_43,
            // "contents", searcher.getCore().getSchema().getAnalyzer());

            SuggestionResultSet newrs = new SuggestionResultSet("", maxPhraseSearch);
            StringBuilder sb = new StringBuilder();
            // build a search in all of the target fields
            for (int zz = 0; zz < queryTokens.length - 1; zz++) {
                newrs.myval = newrs.myval + queryTokens[zz] + " ";
                StringBuilder inner = new StringBuilder();
                for (String field : fields) {
                    String escaped_field = parser.escape(field);
                    // looking for the query token
                    String escaped_token = parser.escape(queryTokens[zz]);
                    inner.append(escaped_field + ":" + escaped_token + " ");
                }
                if (inner.length() > 0) {
                    sb.append("+(" + inner + ")");
                }
            }

            // LOGGER.info("SB query:\t" + sb.toString());
            Query q = null;
            try {
                // convert it to a solr query
                q = parser.parse(sb.toString());
                // LOGGER.info("BQ1 query:\t" + q.toString());
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.error("Error parsing query:\t" + sb.toString());
            }
            DocSet qd = searcher.getDocSet(q);
            // LOGGER.info("Number of docs in set\t" + qd.size());

            for (SuggestionResult sr : rs.suggestions) {
                // for each of the possible suggestions, see how prevelant
                // they are in the document set so that we can know their
                // likelihood of being correct
                sb = new StringBuilder();
                // should use tokensizer
                String[] suggestionTokens = sr.suggestion.split(" ");

                for (int zz = 0; zz < suggestionTokens.length; zz++) {
                    StringBuilder inner = new StringBuilder();
                    for (String field : fields) {
                        inner.append(field + ":" + suggestionTokens[zz] + " ");
                    }
                    if (inner.length() > 0) {
                        sb.append("+(" + inner + ")");
                    }
                }

                // prevent zero bump down
                double Q_c = .0000001;

                try {
                    // LOGGER.info("BQ2 query String:\t" + sb.toString());
                    q = parser.parse(sb.toString());
                    // LOGGER.info("BQ2 query query:\t" + q.toString());
                } catch (Exception e) {
                    // LOGGER.error("parser fail?");
                }

                DocSet pd = searcher.getDocSet(q);

                // LOGGER.info("Number of docs in phrase set\t" +
                // pd.size());
                if (pd.size() != 0) {
                    // As per equation (13) from paper
                    Q_c += qd.intersection(pd).size() / (pd.size() * 1.0);
                }
                // LOGGER.info("Number of docs in phrase set----- Q_c\t (" +
                // Q_c + ") * (" + sr.probability + ")");
                newrs.add(sr.suggestion, sr.probability * Q_c);
            }
            rs = newrs;
        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage());
    }
    return rs;
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

@Override
public void indexNode(Node node, boolean overwrite) throws IOException, AuthenticationException, JSONException {
    RefCounted<SolrIndexSearcher> refCounted = null;
    try {/*  w w  w .  j av  a  2 s  . c o m*/
        long start = System.nanoTime();

        refCounted = core.getSearcher(false, true, null);
        SolrIndexSearcher solrIndexSearcher = refCounted.get();

        if ((node.getStatus() == SolrApiNodeStatus.DELETED)
                || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {
            // fix up any secondary paths
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(node.getId());
            nmdp.setToNodeId(node.getId());
            List<NodeMetaData> nodeMetaDatas;
            if (node.getStatus() == SolrApiNodeStatus.DELETED) {
                // Fake the empty node metadata for this parent deleted node
                NodeMetaData nodeMetaData = new NodeMetaData();
                nodeMetaData.setId(node.getId());
                nodeMetaData.setType(ContentModel.TYPE_DELETED);
                nodeMetaData.setNodeRef(new NodeRef(node.getNodeRef()));
                nodeMetaData.setTxnId(node.getTxnId());
                nodeMetaDatas = Collections.singletonList(nodeMetaData);
            } else {
                nodeMetaDatas = coreTracker.getNodesMetaData(nmdp, MAX_RESULTS_NODES_META_DATA);
            }
            for (NodeMetaData nodeMetaData : nodeMetaDatas) {
                if (nodeMetaData.getTxnId() > node.getTxnId()) {
                    // the node has moved on to a later transaction
                    // it will be indexed later
                    continue;
                }
                LinkedHashSet<Long> visited = new LinkedHashSet<Long>();
                updateDescendantAuxDocs(nodeMetaData, overwrite, solrIndexSearcher, visited,
                        solrIndexSearcher.getDocSet(skippingDocsQuery));
            }

            log.debug(".. deleting");
            DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
            docCmd.id = "LEAF-" + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

            docCmd = new DeleteUpdateCommand();
            docCmd.id = "AUX-" + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

            docCmd = new DeleteUpdateCommand();
            docCmd.id = "UNINDEXED-" + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

            docCmd = new DeleteUpdateCommand();
            docCmd.id = PREFIX_ERROR + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

        }

        if ((node.getStatus() == SolrApiNodeStatus.UPDATED)
                || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {

            log.info(".. updating");
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(node.getId());
            nmdp.setToNodeId(node.getId());

            List<NodeMetaData> nodeMetaDatas = coreTracker.getNodesMetaData(nmdp, MAX_RESULTS_NODES_META_DATA);

            AddUpdateCommand leafDocCmd = new AddUpdateCommand();
            leafDocCmd.overwriteCommitted = overwrite;
            leafDocCmd.overwritePending = overwrite;
            AddUpdateCommand auxDocCmd = new AddUpdateCommand();
            auxDocCmd.overwriteCommitted = overwrite;
            auxDocCmd.overwritePending = overwrite;

            ArrayList<Reader> toClose = new ArrayList<Reader>();
            ArrayList<File> toDelete = new ArrayList<File>();

            for (NodeMetaData nodeMetaData : nodeMetaDatas) {
                if (nodeMetaData.getTxnId() > node.getTxnId()) {
                    // the node has moved on to a later transaction
                    // it will be indexed later
                    continue;
                }

                if (mayHaveChildren(nodeMetaData)) {
                    log.info(".. checking for path change");
                    BooleanQuery bQuery = new BooleanQuery();
                    bQuery.add(new TermQuery(
                            new Term(QueryConstants.FIELD_DBID, NumericEncoder.encode(nodeMetaData.getId()))),
                            Occur.MUST);
                    bQuery.add(new TermQuery(new Term(QueryConstants.FIELD_PARENT_ASSOC_CRC,
                            NumericEncoder.encode(nodeMetaData.getParentAssocsCrc()))), Occur.MUST);
                    DocSet docSet = solrIndexSearcher.getDocSet(bQuery);
                    if (docSet.size() > 0) {
                        log.debug("... found aux match");
                    } else {
                        docSet = solrIndexSearcher.getDocSet(new TermQuery(new Term(QueryConstants.FIELD_DBID,
                                NumericEncoder.encode(nodeMetaData.getId()))));
                        if (docSet.size() > 0) {
                            log.debug("... cascade updating aux doc");
                            LinkedHashSet<Long> visited = new LinkedHashSet<Long>();
                            updateDescendantAuxDocs(nodeMetaData, overwrite, solrIndexSearcher, visited,
                                    solrIndexSearcher.getDocSet(skippingDocsQuery));
                        } else {
                            log.debug("... no aux doc");
                        }
                    }
                }

                Map<QName, PropertyValue> properties = nodeMetaData.getProperties();

                // check index control

                if (properties.containsKey(ContentModel.PROP_IS_INDEXED)) {
                    StringPropertyValue pValue = (StringPropertyValue) properties
                            .get(ContentModel.PROP_IS_INDEXED);
                    if (pValue != null) {
                        Boolean isIndexed = Boolean.valueOf(pValue.getValue());
                        if ((isIndexed != null) && (isIndexed.booleanValue() == false)) {
                            DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
                            docCmd.id = "LEAF-" + node.getId();
                            docCmd.fromPending = true;
                            docCmd.fromCommitted = true;
                            core.getUpdateHandler().delete(docCmd);

                            docCmd = new DeleteUpdateCommand();
                            docCmd.id = "AUX-" + node.getId();
                            docCmd.fromPending = true;
                            docCmd.fromCommitted = true;
                            core.getUpdateHandler().delete(docCmd);

                            docCmd = new DeleteUpdateCommand();
                            docCmd.id = PREFIX_ERROR + node.getId();
                            docCmd.fromPending = true;
                            docCmd.fromCommitted = true;
                            core.getUpdateHandler().delete(docCmd);

                            SolrInputDocument doc = new SolrInputDocument();
                            doc.addField(QueryConstants.FIELD_ID, "UNINDEXED-" + nodeMetaData.getId());
                            doc.addField(QueryConstants.FIELD_DBID, nodeMetaData.getId());
                            doc.addField(QueryConstants.FIELD_LID, nodeMetaData.getNodeRef());
                            doc.addField(QueryConstants.FIELD_INTXID, nodeMetaData.getTxnId());

                            leafDocCmd.solrDoc = doc;
                            leafDocCmd.doc = toDocument(leafDocCmd.getSolrInputDocument(), core.getSchema(),
                                    dataModel);

                            if (leafDocCmd.doc != null && recordUnindexedNodes) {
                                core.getUpdateHandler().addDoc(leafDocCmd);
                            }

                            long end = System.nanoTime();
                            coreTracker.getTrackerStats().addNodeTime(end - start);
                            return;
                        }
                    }
                }

                boolean isContentIndexedForNode = true;
                if (properties.containsKey(ContentModel.PROP_IS_CONTENT_INDEXED)) {
                    StringPropertyValue pValue = (StringPropertyValue) properties
                            .get(ContentModel.PROP_IS_CONTENT_INDEXED);
                    if (pValue != null) {
                        Boolean isIndexed = Boolean.valueOf(pValue.getValue());
                        if ((isIndexed != null) && (isIndexed.booleanValue() == false)) {
                            isContentIndexedForNode = false;
                        }
                    }
                }

                // Make sure any unindexed doc is removed.
                DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
                docCmd.id = "UNINDEXED-" + node.getId();
                docCmd.fromPending = true;
                docCmd.fromCommitted = true;
                core.getUpdateHandler().delete(docCmd);

                docCmd = new DeleteUpdateCommand();
                docCmd.id = PREFIX_ERROR + node.getId();
                docCmd.fromPending = true;
                docCmd.fromCommitted = true;
                core.getUpdateHandler().delete(docCmd);

                SolrInputDocument doc = new SolrInputDocument();
                doc.addField(QueryConstants.FIELD_ID, "LEAF-" + nodeMetaData.getId());
                doc.addField(QueryConstants.FIELD_DBID, nodeMetaData.getId());
                doc.addField(QueryConstants.FIELD_LID, nodeMetaData.getNodeRef());
                doc.addField(QueryConstants.FIELD_INTXID, nodeMetaData.getTxnId());

                for (QName propertyQname : properties.keySet()) {
                    if (dataModel.isIndexedOrStored(propertyQname)) {
                        PropertyValue value = properties.get(propertyQname);
                        if (value != null) {
                            if (value instanceof ContentPropertyValue) {
                                addContentPropertyToDoc(doc, toClose, toDelete, nodeMetaData, propertyQname,
                                        (ContentPropertyValue) value, isContentIndexedForNode);
                            } else if (value instanceof MLTextPropertyValue) {
                                addMLTextPropertyToDoc(doc, propertyQname, (MLTextPropertyValue) value);
                            } else if (value instanceof MultiPropertyValue) {
                                MultiPropertyValue typedValue = (MultiPropertyValue) value;
                                for (PropertyValue singleValue : typedValue.getValues()) {
                                    if (singleValue instanceof ContentPropertyValue) {
                                        addContentPropertyToDoc(doc, toClose, toDelete, nodeMetaData,
                                                propertyQname, (ContentPropertyValue) singleValue,
                                                isContentIndexedForNode);
                                    } else if (singleValue instanceof MLTextPropertyValue) {
                                        addMLTextPropertyToDoc(doc, propertyQname,
                                                (MLTextPropertyValue) singleValue);

                                    } else if (singleValue instanceof StringPropertyValue) {
                                        addStringPropertyToDoc(doc, propertyQname,
                                                (StringPropertyValue) singleValue, properties);
                                    }
                                }
                            } else if (value instanceof StringPropertyValue) {
                                addStringPropertyToDoc(doc, propertyQname, (StringPropertyValue) value,
                                        properties);
                            }

                        }
                    }
                }
                doc.addField(QueryConstants.FIELD_TYPE, nodeMetaData.getType().toString());
                for (QName aspect : nodeMetaData.getAspects()) {
                    doc.addField(QueryConstants.FIELD_ASPECT, aspect.toString());
                }
                doc.addField(QueryConstants.FIELD_ISNODE, "T");
                doc.addField(QueryConstants.FIELD_FTSSTATUS, "Clean");
                // TODO: Place holder to test tenant queries
                String tenant = nodeMetaData.getTenantDomain();
                if (tenant.length() > 0) {
                    doc.addField(QueryConstants.FIELD_TENANT, nodeMetaData.getTenantDomain());
                } else {
                    doc.addField(QueryConstants.FIELD_TENANT, "_DEFAULT_");
                }

                leafDocCmd.solrDoc = doc;
                leafDocCmd.doc = toDocument(leafDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

                SolrInputDocument aux = createAuxDoc(nodeMetaData);
                auxDocCmd.solrDoc = aux;
                auxDocCmd.doc = toDocument(auxDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

            }

            if (leafDocCmd.doc != null) {
                core.getUpdateHandler().addDoc(leafDocCmd);
            }
            if (auxDocCmd.doc != null) {
                core.getUpdateHandler().addDoc(auxDocCmd);
            }

            for (Reader forClose : toClose) {
                try {
                    forClose.close();
                } catch (IOException ioe) {
                }

            }

            for (File file : toDelete) {
                file.delete();
            }

        }
        long end = System.nanoTime();
        coreTracker.getTrackerStats().addNodeTime(end - start);
    } catch (Exception e) {
        // generic recovery
        // Add failed node marker to try later
        // TODO: add to reporting
        // TODO: Store exception for display via query
        // TODO: retry failed

        DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
        docCmd.id = "LEAF-" + node.getId();
        docCmd.fromPending = true;
        docCmd.fromCommitted = true;
        core.getUpdateHandler().delete(docCmd);

        docCmd = new DeleteUpdateCommand();
        docCmd.id = "AUX-" + node.getId();
        docCmd.fromPending = true;
        docCmd.fromCommitted = true;
        core.getUpdateHandler().delete(docCmd);

        docCmd = new DeleteUpdateCommand();
        docCmd.id = "UNINDEXED-" + node.getId();
        docCmd.fromPending = true;
        docCmd.fromCommitted = true;
        core.getUpdateHandler().delete(docCmd);

        AddUpdateCommand leafDocCmd = new AddUpdateCommand();
        leafDocCmd.overwriteCommitted = overwrite;
        leafDocCmd.overwritePending = overwrite;

        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(QueryConstants.FIELD_ID, PREFIX_ERROR + node.getId());
        doc.addField(QueryConstants.FIELD_DBID, node.getId());
        doc.addField(QueryConstants.FIELD_INTXID, node.getTxnId());
        doc.addField(QueryConstants.FIELD_EXCEPTION_MESSAGE, e.getMessage());

        StringWriter stringWriter = new StringWriter(4096);
        PrintWriter printWriter = new PrintWriter(stringWriter, true);
        try {
            e.printStackTrace(printWriter);
            doc.addField(QueryConstants.FIELD_EXCEPTION_STACK, stringWriter.toString());
        } finally {
            printWriter.close();
        }

        leafDocCmd.solrDoc = doc;
        leafDocCmd.doc = toDocument(leafDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

        if (leafDocCmd.doc != null) {
            core.getUpdateHandler().addDoc(leafDocCmd);
        }

        log.warn("Node index failed and skipped for " + node.getId() + " in Tx " + node.getTxnId(), e);
    } finally {
        if (refCounted != null) {
            refCounted.decref();
        }
    }

}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

private void doUpdateDescendantAuxDocs(NodeMetaData parentNodeMetaData, boolean overwrite,
        SolrIndexSearcher solrIndexSearcher, LinkedHashSet<Long> stack, DocSet skippingDocs)
        throws AuthenticationException, IOException, JSONException {
    if ((skipDescendantAuxDocsForSpecificTypes
            && typesForSkippingDescendantAuxDocs.contains(parentNodeMetaData.getType()))
            || (skipDescendantAuxDocsForSpecificAspects
                    && shouldBeIgnoredByAnyAspect(parentNodeMetaData.getAspects()))) {
        return;/*w w w  .  ja va 2  s  . co m*/
    }

    HashSet<Long> childIds = new HashSet<Long>();

    if (parentNodeMetaData.getChildIds() != null) {
        childIds.addAll(parentNodeMetaData.getChildIds());
    }

    BooleanQuery bQuery = new BooleanQuery();
    bQuery.add(new TermQuery(new Term(QueryConstants.FIELD_PARENT, parentNodeMetaData.getNodeRef().toString())),
            Occur.MUST);
    DocSet docSet = solrIndexSearcher.getDocSet(bQuery);
    ResizeableArrayList<CacheEntry> indexedByDocId = (ResizeableArrayList<CacheEntry>) solrIndexSearcher
            .cacheLookup(AlfrescoSolrEventListener.ALFRESCO_ARRAYLIST_CACHE,
                    AlfrescoSolrEventListener.KEY_DBID_LEAF_PATH_BY_DOC_ID);
    if (docSet instanceof BitDocSet) {
        BitDocSet source = (BitDocSet) docSet;
        OpenBitSet openBitSet = source.getBits();
        int current = -1;
        while ((current = openBitSet.nextSetBit(current + 1)) != -1) {
            if (!skippingDocs.exists(current)) {
                CacheEntry entry = indexedByDocId.get(current);
                childIds.add(entry.getDbid());
            }
        }
    } else {
        for (DocIterator it = docSet.iterator(); it.hasNext(); /* */) {
            int current = it.nextDoc();
            if (!skippingDocs.exists(current)) {
                CacheEntry entry = indexedByDocId.get(current);
                childIds.add(entry.getDbid());
            }
        }
    }

    for (Long childId : childIds) {
        if (!shouldElementBeIgnored(childId, solrIndexSearcher, skippingDocs)) {
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(childId);
            nmdp.setToNodeId(childId);
            nmdp.setIncludeAclId(true);
            nmdp.setIncludeAspects(true);
            nmdp.setIncludeChildAssociations(false);
            nmdp.setIncludeChildIds(true);
            nmdp.setIncludeNodeRef(true);
            nmdp.setIncludeOwner(true);
            nmdp.setIncludeParentAssociations(true);
            nmdp.setIncludePaths(true);
            nmdp.setIncludeProperties(false);
            nmdp.setIncludeType(true);
            nmdp.setIncludeTxnId(true);
            // call back to core tracker to talk to client
            List<NodeMetaData> nodeMetaDatas = coreTracker.getNodesMetaData(nmdp, MAX_RESULTS_NODES_META_DATA);

            for (NodeMetaData nodeMetaData : nodeMetaDatas) {
                if (mayHaveChildren(nodeMetaData)) {
                    updateDescendantAuxDocs(nodeMetaData, overwrite, solrIndexSearcher, stack, skippingDocs);
                }

                // Avoid adding aux docs for stuff yet to be indexed or unindexed (via the index control aspect)
                log.info(".. checking aux doc exists in index before we update it");
                Query query = new TermQuery(new Term(QueryConstants.FIELD_ID, "AUX-" + childId));
                DocSet auxSet = solrIndexSearcher.getDocSet(query);
                if (auxSet.size() > 0) {
                    log.debug("... cascade update aux doc " + childId);

                    SolrInputDocument aux = createAuxDoc(nodeMetaData);
                    AddUpdateCommand auxDocCmd = new AddUpdateCommand();
                    auxDocCmd.overwriteCommitted = overwrite;
                    auxDocCmd.overwritePending = overwrite;
                    auxDocCmd.solrDoc = aux;
                    auxDocCmd.doc = toDocument(auxDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

                    core.getUpdateHandler().addDoc(auxDocCmd);
                } else {
                    log.debug("... no aux doc found to update " + childId);
                }
            }
        }
    }
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

@Override
public int getDocSetSize(String targetTxId, String targetTxCommitTime) throws IOException {
    RefCounted<SolrIndexSearcher> refCounted = null;
    try {/*from  w  ww  .ja v a  2  s  .  c o m*/
        refCounted = core.getSearcher(false, true, null);
        SolrIndexSearcher solrIndexSearcher = refCounted.get();
        BooleanQuery query = new BooleanQuery();
        query.add(new TermQuery(new Term(QueryConstants.FIELD_TXID, targetTxId)), Occur.MUST);
        query.add(new TermQuery(new Term(QueryConstants.FIELD_TXCOMMITTIME, targetTxCommitTime)), Occur.MUST);
        DocSet set = solrIndexSearcher.getDocSet(query);

        return set.size();
    } finally {
        if (refCounted != null) {
            refCounted.decref();
        }
    }
}

From source file:org.alfresco.solr.query.SolrDenySetScorer2.java

License:Open Source License

public static SolrDenySetScorer2 createDenySetScorer(Weight weight, LeafReaderContext context,
        SolrIndexSearcher searcher, String authorities, LeafReader reader) throws IOException {
    DocSet deniedDocSet = (DocSet) searcher.cacheLookup(CacheConstants.ALFRESCO_DENIED_CACHE, authorities);

    if (deniedDocSet == null) {

        String[] auths = authorities.substring(1).split(authorities.substring(0, 1));

        deniedDocSet = new BitDocSet(new FixedBitSet(searcher.maxDoc()));

        BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
        for (String current : auths) {
            bQuery.add(new TermQuery(new Term(QueryConstants.FIELD_DENIED, current)), Occur.SHOULD);
        }/* www.  j  ava  2 s. c  om*/
        WrappedQuery wrapped = new WrappedQuery(bQuery.build());
        wrapped.setCache(false);

        DocSet aclDocs = searcher.getDocSet(wrapped);

        HashSet<Long> aclsFound = new HashSet<Long>(aclDocs.size());
        NumericDocValues aclDocValues = searcher.getSlowAtomicReader()
                .getNumericDocValues(QueryConstants.FIELD_ACLID);

        for (DocIterator it = aclDocs.iterator(); it.hasNext(); /**/) {
            int docID = it.nextDoc();
            // Obtain the ACL ID for this ACL doc.
            long aclID = aclDocValues.get(docID);
            aclsFound.add(getLong(aclID));
        }

        if (aclsFound.size() > 0) {
            for (LeafReaderContext readerContext : searcher.getSlowAtomicReader().leaves()) {
                int maxDoc = readerContext.reader().maxDoc();
                NumericDocValues fieldValues = DocValuesCache.getNumericDocValues(QueryConstants.FIELD_ACLID,
                        readerContext.reader());
                if (fieldValues != null) {
                    for (int i = 0; i < maxDoc; i++) {
                        long aclID = fieldValues.get(i);
                        Long key = getLong(aclID);
                        if (aclsFound.contains(key)) {
                            deniedDocSet.add(readerContext.docBase + i);
                        }
                    }
                }

            }
        }

        // Exclude the ACL docs from the results, we only want real docs that match.
        // Probably not very efficient, what we really want is remove(docID)
        deniedDocSet = deniedDocSet.andNot(aclDocs);
        searcher.cacheInsert(CacheConstants.ALFRESCO_DENIED_CACHE, authorities, deniedDocSet);
    }

    // TODO: cache the full set? e.g. searcher.cacheInsert(CacheConstants.ALFRESCO_READERSET_CACHE, authorities, readableDocSet)
    // plus check of course, for presence in cache at start of method.
    return new SolrDenySetScorer2(weight, deniedDocSet, context, searcher);

}

From source file:org.alfresco.solr.query.SolrReaderSetScorer2.java

License:Open Source License

public static AbstractSolrCachingScorer createReaderSetScorer(Weight weight, LeafReaderContext context,
        SolrIndexSearcher searcher, String authorities, LeafReader reader) throws IOException {

    DocSet readableDocSet = (DocSet) searcher.cacheLookup(CacheConstants.ALFRESCO_READER_CACHE, authorities);

    if (readableDocSet == null) {

        String[] auths = authorities.substring(1).split(authorities.substring(0, 1));

        readableDocSet = new BitDocSet(new FixedBitSet(searcher.maxDoc()));

        BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
        for (String current : auths) {
            bQuery.add(new TermQuery(new Term(QueryConstants.FIELD_READER, current)), Occur.SHOULD);
        }//from ww w  . j a v a  2s.  com
        WrappedQuery wrapped = new WrappedQuery(bQuery.build());
        wrapped.setCache(false);

        DocSet aclDocs = searcher.getDocSet(wrapped);

        HashSet<Long> aclsFound = new HashSet<Long>(aclDocs.size());
        NumericDocValues aclDocValues = searcher.getSlowAtomicReader()
                .getNumericDocValues(QueryConstants.FIELD_ACLID);

        for (DocIterator it = aclDocs.iterator(); it.hasNext(); /**/) {
            int docID = it.nextDoc();
            // Obtain the ACL ID for this ACL doc.
            long aclID = aclDocValues.get(docID);
            aclsFound.add(getLong(aclID));
        }

        if (aclsFound.size() > 0) {
            for (LeafReaderContext readerContext : searcher.getSlowAtomicReader().leaves()) {
                int maxDoc = readerContext.reader().maxDoc();
                NumericDocValues fieldValues = DocValuesCache.getNumericDocValues(QueryConstants.FIELD_ACLID,
                        readerContext.reader());
                if (fieldValues != null) {
                    for (int i = 0; i < maxDoc; i++) {
                        long aclID = fieldValues.get(i);
                        Long key = getLong(aclID);
                        if (aclsFound.contains(key)) {
                            readableDocSet.add(readerContext.docBase + i);
                        }
                    }
                }

            }
        }

        // Exclude the ACL docs from the results, we only want real docs that match.
        // Probably not very efficient, what we really want is remove(docID)
        readableDocSet = readableDocSet.andNot(aclDocs);
        searcher.cacheInsert(CacheConstants.ALFRESCO_READER_CACHE, authorities, readableDocSet);
    }

    // TODO: cache the full set? e.g. searcher.cacheInsert(CacheConstants.ALFRESCO_READERSET_CACHE, authorities, readableDocSet)
    // plus check of course, for presence in cache at start of method.
    return new SolrReaderSetScorer2(weight, readableDocSet, context, searcher);
}

From source file:org.apache.lucene.search.suggest.hovland.ContextAwarePhraseLookup.java

License:Apache License

private SuggestionResultSet getSuggestions(String query) {
    query = deAccent(query);//w ww. j a  va2  s .c o  m
    /*String[] queryTokens = query.replaceAll("[^A-Za-z0-9 ]", " ")
    .replace("  ", " ").trim().split(" "); */
    String[] queryTokens = SuggesterAnalyzer.analyze(SuggesterAnalyzer.getQueryAnalizer(), query);
    String endToken = queryTokens[queryTokens.length - 1].toLowerCase();
    LOGGER.debug("Building suggestions for: " + endToken);
    SuggestionResultSet rs = headNode.computeQt(endToken, maxPhraseSearch); // get completion for the last word in the
                                                                            // suggestion
                                                                            // didn't find it, bail early
    if (rs == null) {
        LOGGER.debug("Didn't find anyting for: " + endToken);
        return new SuggestionResultSet("", 0);
    }
    LOGGER.debug(rs.toString());

    rs.value = "";
    LOGGER.debug("Doing 2nd part of equation");
    try {

        if (queryTokens.length > 1) {

            QueryParser parser = new QueryParser("contents",
                    searcher.getCore().getLatestSchema().getQueryAnalyzer());
            SuggestionResultSet newRs = new SuggestionResultSet("", maxPhraseSearch);
            StringBuilder sb = new StringBuilder();
            // build a search in all of the target fields
            for (int zz = 0; zz < queryTokens.length - 1; zz++) {
                newRs.value = newRs.value + queryTokens[zz] + " ";
                StringBuilder inner = new StringBuilder();

                for (String field : fields) {
                    String escaped_field = parser.escape(field);
                    // looking for the query token
                    String escaped_token = parser.escape(queryTokens[zz]);
                    inner.append(escaped_field + ":" + escaped_token + " ");
                }
                if (inner.length() > 0) {
                    sb.append("+(" + inner + ")");
                }
            }

            LOGGER.debug("SB query:\t" + sb.toString());
            Query q = null;
            try {
                // convert it to a solr query
                q = parser.parse(sb.toString());
                LOGGER.debug("BQ1 query:\t" + q.toString());
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.error("Error parsing query:\t" + sb.toString());
            }
            DocSet qd = searcher.getDocSet(q);
            LOGGER.debug("Number of docs in set\t" + qd.size());
            int numOfSuggestions = 0;
            for (SuggestionResultSet.SuggestionResult sr : rs.suggestions) {
                // for each of the possible suggestions, see how prevalent
                // they are in the document set so that we can know their
                // likelihood of being correct
                sb = new StringBuilder();
                // should use tokensizer
                String[] suggestionTokens = sr.suggestion.split(" ");

                for (int zz = 0; zz < suggestionTokens.length; zz++) {
                    StringBuilder inner = new StringBuilder();
                    for (String field : fields) {
                        inner.append(field + ":" + suggestionTokens[zz] + " ");
                    }
                    if (inner.length() > 0) {
                        sb.append("+(" + inner + ")");
                    }
                }

                // prevent zero bump down
                double Q_c = .0000001;

                try {
                    LOGGER.debug("BQ2 query String:\t" + sb.toString());
                    q = parser.parse(sb.toString());
                    LOGGER.debug("BQ2 query query:\t" + q.toString());
                } catch (Exception e) {
                    // LOGGER.error("parser fail?");
                }

                DocSet pd = searcher.getDocSet(q);

                LOGGER.debug("Number of docs in phrase set\t" + pd.size());
                if (pd.size() != 0) {
                    // As per equation (13) from paper
                    Q_c += qd.intersection(pd).size() / (pd.size() * 1.0);
                }
                LOGGER.debug("Q_c = (" + Q_c + ") * (" + sr.probability + ")");
                LOGGER.debug("Adding: {" + sr.suggestion + ", " + (sr.probability * Q_c));
                newRs.add(sr.suggestion, sr.probability * Q_c);
            }
            rs = newRs;
            LOGGER.debug("Final suggestions. " + rs.toString());
        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage());
    }
    return rs;
}

From source file:org.apache.lucene.search.suggest.hovland.ContextAwarePhraseSuggester.java

License:Apache License

public SuggestionResultSet getSuggestions(SolrIndexSearcher searcher, String[] fields, String query,
        int maxPhraseSearch) {
    query = deAccent(query);/*from  ww  w  .j  a va  2 s.  co m*/
    /*String[] queryTokens = query.replaceAll("[^A-Za-z0-9 ]", " ")
    .replace("  ", " ").trim().split(" "); */
    String[] queryTokens = SuggesterAnalyzer.analyze(SuggesterAnalyzer.getQueryAnalizer(), query);
    String endToken = queryTokens[queryTokens.length - 1].toLowerCase();
    LOGGER.debug("Building suggestions for: " + endToken);
    SuggestionResultSet rs = headNode.computeQt(endToken, maxPhraseSearch); // get completion for the last word in the
                                                                            // suggestion
                                                                            // didn't find it, bail early
    if (rs == null) {
        LOGGER.debug("Didn't find anyting for: " + endToken);
        return new SuggestionResultSet("", 0);
    }
    LOGGER.debug(rs.toString());

    rs.value = "";
    LOGGER.debug("Doing 2nd part of equation");
    try {

        if (queryTokens.length > 1) {

            QueryParser parser = new QueryParser("contents",
                    searcher.getCore().getLatestSchema().getQueryAnalyzer());
            SuggestionResultSet newRs = new SuggestionResultSet("", maxPhraseSearch);
            StringBuilder sb = new StringBuilder();
            // build a search in all of the target fields
            for (int zz = 0; zz < queryTokens.length - 1; zz++) {
                newRs.value = newRs.value + queryTokens[zz] + " ";
                StringBuilder inner = new StringBuilder();

                for (String field : fields) {
                    String escaped_field = parser.escape(field);
                    // looking for the query token
                    String escaped_token = parser.escape(queryTokens[zz]);
                    inner.append(escaped_field + ":" + escaped_token + " ");
                }
                if (inner.length() > 0) {
                    sb.append("+(" + inner + ")");
                }
            }

            LOGGER.debug("SB query:\t" + sb.toString());
            Query q = null;
            try {
                // convert it to a solr query
                q = parser.parse(sb.toString());
                LOGGER.debug("BQ1 query:\t" + q.toString());
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.error("Error parsing query:\t" + sb.toString());
            }
            DocSet qd = searcher.getDocSet(q);
            LOGGER.debug("Number of docs in set\t" + qd.size());
            int numOfSuggestions = 0;
            for (SuggestionResultSet.SuggestionResult sr : rs.suggestions) {
                // for each of the possible suggestions, see how prevalent
                // they are in the document set so that we can know their
                // likelihood of being correct
                sb = new StringBuilder();
                // should use tokensizer
                String[] suggestionTokens = sr.suggestion.split(" ");

                for (int zz = 0; zz < suggestionTokens.length; zz++) {
                    StringBuilder inner = new StringBuilder();
                    for (String field : fields) {
                        inner.append(field + ":" + suggestionTokens[zz] + " ");
                    }
                    if (inner.length() > 0) {
                        sb.append("+(" + inner + ")");
                    }
                }

                // prevent zero bump down
                double Q_c = .0000001;

                try {
                    LOGGER.debug("BQ2 query String:\t" + sb.toString());
                    q = parser.parse(sb.toString());
                    LOGGER.debug("BQ2 query query:\t" + q.toString());
                } catch (Exception e) {
                    // LOGGER.error("parser fail?");
                }

                DocSet pd = searcher.getDocSet(q);

                LOGGER.debug("Number of docs in phrase set\t" + pd.size());
                if (pd.size() != 0) {
                    // As per equation (13) from paper
                    Q_c += qd.intersection(pd).size() / (pd.size() * 1.0);
                }
                LOGGER.debug("Q_c = (" + Q_c + ") * (" + sr.probability + ")");
                LOGGER.debug("Adding: {" + sr.suggestion + ", " + (sr.probability * Q_c));
                newRs.add(sr.suggestion, sr.probability * Q_c);
            }
            rs = newRs;
            LOGGER.debug("Final suggestions. " + rs.toString());
        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage());
    }
    return rs;
}