Example usage for org.apache.lucene.util BitSet set

List of usage examples for org.apache.lucene.util BitSet set

Introduction

In this page you can find the example usage for org.apache.lucene.util BitSet set.

Prototype

public abstract void set(int i);

Source Link

Document

Set the bit at i.

Usage

From source file:de.unihildesheim.iw.lucene.search.EmptyFieldFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(@NotNull final LeafReaderContext context, @Nullable final Bits acceptDocs)
        throws IOException {
    FixedBitSet checkBits;/*from  w ww  .  jav a  2s  .co m*/
    final LeafReader reader = context.reader();
    final int maxDoc = reader.maxDoc();

    BitSet finalBits = new SparseFixedBitSet(maxDoc);
    if (acceptDocs == null) {
        checkBits = BitsUtils.bits2FixedBitSet(reader.getLiveDocs());
        if (checkBits == null) {
            // all live
            checkBits = new FixedBitSet(maxDoc);
            checkBits.set(0, checkBits.length());
        }
    } else {
        checkBits = BitsUtils.bits2FixedBitSet(acceptDocs);
    }

    @Nullable
    final Terms terms = reader.terms(this.field);
    if (terms != null) {
        final int termsDocCount = terms.getDocCount();

        if (termsDocCount != 0) {
            if (termsDocCount == maxDoc) {
                // all matching
                finalBits = checkBits;
            } else {
                @Nullable
                final Terms t = reader.terms(this.field);
                if (t != null) {
                    PostingsEnum pe = null;
                    final TermsEnum te = t.iterator(null);
                    int docId;
                    while (te.next() != null) {
                        pe = te.postings(checkBits, pe, (int) PostingsEnum.NONE);
                        while ((docId = pe.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
                            if (checkBits.getAndClear(docId)) {
                                finalBits.set(docId);
                            }
                        }
                    }
                }
            }
        }
    }
    return new BitDocIdSet(finalBits);
}

From source file:de.unihildesheim.iw.lucene.search.IPCFieldFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(@NotNull final LeafReaderContext context, @Nullable final Bits acceptDocs)
        throws IOException {
    final LeafReader reader = context.reader();
    final int maxDoc = reader.maxDoc();
    final BitSet finalBits = new SparseFixedBitSet(maxDoc);

    if (acceptDocs == null) {
        // check all
        for (int i = 0; i < maxDoc; i++) {
            if (this.filterFunc.isAccepted(reader, i, this.ipcParser)) {
                finalBits.set(i);
            }//from  w  ww  .jav a2 s  .  co m
        }
    } else {
        final BitSet checkBits = BitsUtils.bits2BitSet(acceptDocs);
        final DocIdSetIterator disi = new BitDocIdSet(checkBits).iterator();
        int docId;
        while ((docId = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            if (this.filterFunc.isAccepted(reader, docId, this.ipcParser)) {
                finalBits.set(docId);
            }
        }
    }

    return new BitDocIdSet(finalBits);
}

From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtilsTest.java

License:Open Source License

@Test
public void testMaxDoc_first() throws Exception {
    final BitSet bits = new FixedBitSet(10);
    bits.set(0);

    Assert.assertEquals("Cardinality mismatch", 0L, (long) DocIdSetUtils.maxDoc(new BitDocIdSet(bits)));
}

From source file:org.apache.jackrabbit.core.query.lucene.JahiaLuceneQueryFactoryImpl.java

License:Open Source License

/**
 * Override LuceneQueryFactory.execute()
 */// w w  w .  j a  v  a 2s.  c o m
@Override
public List<Row> execute(Map<String, PropertyValue> columns, Selector selector, Constraint constraint,
        Sort sort, boolean externalSort, long offsetIn, long limitIn) throws RepositoryException, IOException {
    final IndexReader reader = index.getIndexReader(true);
    final int offset = offsetIn < 0 ? 0 : (int) offsetIn;
    final int limit = limitIn < 0 ? Integer.MAX_VALUE : (int) limitIn;

    QueryHits hits = null;
    try {
        JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader,
                index.getContext().getItemStateManager());
        searcher.setSimilarity(index.getSimilarity());

        Predicate filter = Predicate.TRUE;
        BooleanQuery query = new BooleanQuery();

        QueryPair qp = new QueryPair(query);

        query.add(create(selector), MUST);
        if (constraint != null) {
            String name = selector.getSelectorName();
            NodeType type = ntManager.getNodeType(selector.getNodeTypeName());
            filter = mapConstraintToQueryAndFilter(qp, constraint, Collections.singletonMap(name, type),
                    searcher, reader);
        }

        // Added by jahia
        Set<String> foundIds = new HashSet<String>();
        int hasFacets = FacetHandler.hasFacetFunctions(columns, session);
        CountHandler.CountType countType = CountHandler.hasCountFunction(columns, session);
        boolean isCount = countType != null;
        BitSet bitset = (hasFacets & FacetHandler.FACET_COLUMNS) == 0 ? null : new BitSet();
        // End

        List<Row> rowList = externalSort ? new LinkedList<Row>() : null;
        Map<String, Row> rows = externalSort ? null : new LinkedHashMap<String, Row>();
        hits = searcher.evaluate(qp.mainQuery, sort, offset + limit);
        int currentNode = 0;
        int addedNodes = 0;
        int resultCount = 0;
        int hitsSize = 0;

        ScoreNode node = hits.nextScoreNode();
        Map<String, Boolean> checkedAcls = new HashMap<String, Boolean>();

        while (node != null) {
            if (isCount && countType.isApproxCount()) {
                hitsSize++;
                if (hitsSize > countType.getApproxCountLimit()) {
                    if (hits.getSize() > 0) {
                        hitsSize = hits.getSize();
                        break;
                    } else {
                        node = hits.nextScoreNode();
                        continue;
                    }
                }
            }
            IndexedNodeInfo infos = getIndexedNodeInfo(node, reader, isCount && countType.isSkipChecks());
            if (foundIds.add(infos.getMainNodeUuid())) { // <-- Added by jahia
                if (isCount && countType.isSkipChecks()) {
                    resultCount++;
                } else {
                    try {
                        boolean canRead = true;
                        if (isAclUuidInIndex()) {
                            canRead = checkIndexedAcl(checkedAcls, infos);
                        }
                        boolean checkVisibility = "1".equals(infos.getCheckVisibility())
                                && Constants.LIVE_WORKSPACE.equals(session.getWorkspace().getName());

                        if (canRead && (!Constants.LIVE_WORKSPACE.equals(session.getWorkspace().getName())
                                || ((infos.getPublished() == null || "true".equals(infos.getPublished()))
                                        && (infos.getCheckInvalidLanguages() == null || getLocale() == null
                                                || !infos.getCheckInvalidLanguages()
                                                        .contains(getLocale().toString()))))) {
                            if (filter == Predicate.TRUE) { // <-- Added by jahia
                                if ((hasFacets & FacetHandler.ONLY_FACET_COLUMNS) == 0) {
                                    Row row = null;

                                    if (checkVisibility || !isAclUuidInIndex()) {
                                        NodeImpl objectNode = getNodeWithAclAndVisibilityCheck(node,
                                                checkVisibility);
                                        if (isCount) {
                                            resultCount++;
                                        } else {
                                            row = new LazySelectorRow(columns, evaluator,
                                                    selector.getSelectorName(), objectNode, node.getScore());
                                        }
                                    } else {
                                        if (isCount) {
                                            resultCount++;
                                        } else {
                                            row = new LazySelectorRow(columns, evaluator,
                                                    selector.getSelectorName(), node.getNodeId(),
                                                    node.getScore());
                                        }
                                    }

                                    if (row == null) {
                                        continue;
                                    }

                                    if (externalSort) {
                                        rowList.add(row);
                                    } else {
                                        // apply limit and offset rules locally
                                        if (currentNode >= offset && currentNode - offset < limit) {
                                            rows.put(node.getNodeId().toString(), row);
                                            addedNodes++;
                                        }
                                        currentNode++;
                                        // end the loop when going over the limit
                                        if (addedNodes == limit) {
                                            break;
                                        }
                                    }
                                }
                                if ((hasFacets & FacetHandler.FACET_COLUMNS) == FacetHandler.FACET_COLUMNS) {
                                    //Added by Jahia
                                    //can be added to bitset when ACL checked and not in live mode or no visibility rule to check
                                    if (isAclUuidInIndex() && !checkVisibility) {
                                        bitset.set(infos.getDocNumber());
                                    } else { //try to load nodeWrapper to check the visibility rules
                                        NodeImpl objectNode = getNodeWithAclAndVisibilityCheck(node,
                                                checkVisibility);
                                        bitset.set(infos.getDocNumber());
                                    }
                                    //!Added by Jahia
                                }
                            } else {
                                NodeImpl objectNode = session.getNodeById(node.getNodeId());
                                if (objectNode.isNodeType("jnt:translation")) {
                                    objectNode = (NodeImpl) objectNode.getParent();
                                }
                                if (isCount) {
                                    resultCount++;
                                } else {
                                    Row row = new SelectorRow(columns, evaluator, selector.getSelectorName(),
                                            objectNode, node.getScore());
                                    if (filter.evaluate(row)) {
                                        if ((hasFacets & FacetHandler.ONLY_FACET_COLUMNS) == 0) {
                                            if (externalSort) {
                                                rowList.add(row);
                                            } else {
                                                // apply limit and offset rules locally
                                                if (currentNode >= offset && currentNode - offset < limit) {
                                                    rows.put(node.getNodeId().toString(), row);
                                                    addedNodes++;
                                                }
                                                currentNode++;
                                                // end the loop when going over the limit
                                                if (addedNodes == limit) {
                                                    break;
                                                }
                                            }
                                        }
                                        if ((hasFacets
                                                & FacetHandler.FACET_COLUMNS) == FacetHandler.FACET_COLUMNS) {
                                            bitset.set(infos.getDocNumber()); // <-- Added by jahia
                                        }
                                    }
                                }
                            }
                        }
                    } catch (PathNotFoundException e) {
                    } catch (ItemNotFoundException e) {
                        // skip the node
                    }
                }
            } else {
                if (((hasFacets & FacetHandler.ONLY_FACET_COLUMNS) == 0) && !isCount && !externalSort
                        && !infos.getMainNodeUuid().equals(node.getNodeId().toString())
                        && rows.containsKey(infos.getMainNodeUuid())) {
                    // we've got the translation node -> adjusting the position of the original node in the result list
                    rows.put(infos.getMainNodeUuid(), rows.remove(infos.getMainNodeUuid()));
                }
            } // <-- Added by jahia
            node = hits.nextScoreNode();
        }

        if (rowList == null) {
            if (rows != null) {
                rowList = new LinkedList<Row>(rows.values());
            } else {
                rowList = new LinkedList<Row>();
            }
        }
        // Added by jahia
        if ((hasFacets & FacetHandler.FACET_COLUMNS) == FacetHandler.FACET_COLUMNS) {
            OpenBitSet docIdSet = new OpenBitSetDISI(new DocIdBitSet(bitset).iterator(), bitset.size());

            FacetHandler h = new FacetHandler(columns, selector, docIdSet, index, session, nsMappings);
            h.handleFacets(reader);
            rowList.add(0, h.getFacetsRow());
        } else if (isCount) {
            boolean wasApproxLimitReached = false;
            if (countType.isApproxCount() && hitsSize > countType.getApproxCountLimit()) {
                resultCount = hitsSize * resultCount / countType.getApproxCountLimit();
                resultCount = (int) Math.ceil(MathUtils.round(resultCount,
                        resultCount < 1000 ? -1 : (resultCount < 10000 ? -2 : -3), BigDecimal.ROUND_UP));
                wasApproxLimitReached = true;
            }
            rowList.add(0, CountHandler.createCountRow(resultCount, wasApproxLimitReached));
        }
        // End

        return rowList;
    } finally {
        if (hits != null) {
            hits.close();
        }
        Util.closeOrRelease(reader);
    }
}

From source file:org.elasticsearch.index.shard.ShardSplittingQuery.java

License:Apache License

private void markChildDocs(BitSet parentDocs, BitSet matchingDocs) {
    int currentDeleted = 0;
    while (currentDeleted < matchingDocs.length()
            && (currentDeleted = matchingDocs.nextSetBit(currentDeleted)) != DocIdSetIterator.NO_MORE_DOCS) {
        int previousParent = parentDocs.prevSetBit(Math.max(0, currentDeleted - 1));
        for (int i = previousParent + 1; i < currentDeleted; i++) {
            matchingDocs.set(i);
        }/*from  www  .j a va 2  s . c  o  m*/
        currentDeleted++;
    }
}

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

License:Open Source License

private OpenBitSet getDocIdSet(Query query, final String locale) {
    OpenBitSet docIds = null;/*from   w  ww . j ava2 s .co  m*/
    try {
        final BitSet bitset = new BitSet();
        searcher.search(query, new AbstractHitCollector() {
            @Override
            public void collect(int docId, float scorer) {
                if (locale != null) {
                    try {
                        int docMainDocId = getMainDocIdForTranslations(
                                searcher.getIndexReader().document(docId, TRANSLATION_FIELDS), locale);
                        if (docMainDocId != -1) {
                            bitset.set(docMainDocId);
                        }
                    } catch (Exception e) {
                        logger.warn("Error getting index document while faceting", e);
                    }
                }
                bitset.set(docId);
            }

            @Override
            public boolean acceptsDocsOutOfOrder() {
                return true;
            }
        });

        docIds = new OpenBitSetDISI(new DocIdBitSet(bitset).iterator(), bitset.size());
    } catch (IOException e) {
        logger.debug("Can't retrive bitset from hits", e);
    }
    return docIds;
}

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

License:Open Source License

OpenBitSet getPositiveDocSet(Query q, final String locale) throws IOException {
    OpenBitSet answer;/*from   w  w  w  .jav a  2s .  c om*/

    //      if (filterCache != null) {
    //        answer = filterCache.get(q);
    //        if (answer!=null) return answer;
    //      }
    final BitSet bitset = new BitSet();
    searcher.search(q, new AbstractHitCollector() {
        @Override
        public void collect(int docId, float scorer) {
            if (locale != null) {
                try {
                    int docMainDocId = getMainDocIdForTranslations(
                            searcher.getIndexReader().document(docId, TRANSLATION_FIELDS), locale);
                    if (docMainDocId != -1) {
                        bitset.set(docMainDocId);
                    }
                } catch (Exception e) {
                    logger.warn("Error getting index document while faceting", e);
                }
            }
            bitset.set(docId);
        }

        @Override
        public boolean acceptsDocsOutOfOrder() {
            return true;
        }
    });
    answer = new OpenBitSetDISI(new DocIdBitSet(bitset).iterator(), bitset.size());
    //      answer = getDocSetNC(q,null);
    //      if (filterCache != null) filterCache.put(q,answer);
    return answer;
}

From source file:org.voyanttools.trombone.lucene.CorpusMapper.java

License:Open Source License

public BitSet getBitSetFromDocumentIds(Collection<String> documentIds) throws IOException {
    BitSet subBitSet = new SparseFixedBitSet(getLeafReader().numDocs());
    for (String id : documentIds) {
        subBitSet.set(getLuceneIdFromDocumentId(id));
    }/*from  w  w w .  jav a  2  s  .c  o  m*/
    return subBitSet;
}