Example usage for org.apache.lucene.util BitDocIdSet bits

List of usage examples for org.apache.lucene.util BitDocIdSet bits

Introduction

In this page you can find the example usage for org.apache.lucene.util BitDocIdSet bits.

Prototype

@Override
    public BitSet bits() 

Source Link

Usage

From source file:org.elasticsearch.index.search.child.AbstractChildTestCase.java

License:Apache License

static boolean equals(BitDocIdSet expected, BitDocIdSet actual) {
    if (actual == null && expected == null) {
        return true;
    } else if (actual == null || expected == null) {
        return false;
    }//from  w ww . j a v a 2  s .c  o m
    BitSet actualBits = actual.bits();
    BitSet expectedBits = expected.bits();
    if (actualBits.length() != expectedBits.length()) {
        return false;
    }
    for (int i = 0; i < expectedBits.length(); i++) {
        if (expectedBits.get(i) != actualBits.get(i)) {
            return false;
        }
    }
    return true;
}

From source file:org.elasticsearch.index.search.child.AbstractChildTestCase.java

License:Apache License

static String reason(BitDocIdSet actual, BitDocIdSet expected, IndexSearcher indexSearcher) throws IOException {
    StringBuilder builder = new StringBuilder();
    builder.append("expected cardinality:").append(expected.bits().cardinality()).append('\n');
    DocIdSetIterator iterator = expected.iterator();
    for (int doc = iterator.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = iterator.nextDoc()) {
        builder.append("Expected doc[").append(doc).append("] with id value ")
                .append(indexSearcher.doc(doc).get(UidFieldMapper.NAME)).append('\n');
    }//ww w.j  ava 2 s  . c om
    builder.append("actual cardinality: ").append(actual.bits().cardinality()).append('\n');
    iterator = actual.iterator();
    for (int doc = iterator.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = iterator.nextDoc()) {
        builder.append("Actual doc[").append(doc).append("] with id value ")
                .append(indexSearcher.doc(doc).get(UidFieldMapper.NAME)).append('\n');
    }
    return builder.toString();
}

From source file:org.geotoolkit.lucene.filter.LuceneOGCFilter.java

License:Open Source License

/**
 * {@inheritDoc }// w  w  w .j a  v a 2s.  c om
 */
@Override
public DocIdSet getDocIdSet(final LeafReaderContext ctx, final Bits b) throws IOException {

    boolean treeSearch = false;
    boolean reverse = false;
    boolean distanceFilter = false;
    final Set<String> treeMatching = new HashSet<>();
    if (tree != null) {
        /*
         * For distance buffer filter no envelope only mode
         */
        if (filter instanceof DistanceBufferOperator) {
            distanceFilter = true;
            reverse = filter instanceof Beyond;
            final DistanceBufferOperator sp = (DistanceBufferOperator) filter;
            if (sp.getExpression2() instanceof Literal) {
                try {
                    final Literal lit = (Literal) sp.getExpression2();
                    final GeneralEnvelope bound = getExtendedReprojectedEnvelope(lit.getValue(), tree.getCrs(),
                            sp.getDistanceUnits(), sp.getDistance());
                    final int[] resultID = tree.searchID(bound);
                    Arrays.sort(resultID);
                    treeMatching.clear();
                    TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                    for (int id : resultID) {
                        final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                        if (env != null) {
                            treeMatching.add(env.getId());
                        }
                    }
                    treeSearch = true;
                } catch (FactoryException ex) {
                    throw new IOException(ex);
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", sp.getExpression2());
            }

        } else if (filter instanceof BinarySpatialOperator) {
            final BinarySpatialOperator sp = (BinarySpatialOperator) filter;
            if (sp.getExpression2() instanceof Literal) {
                final Literal lit = (Literal) sp.getExpression2();
                final Envelope boundFilter = getReprojectedEnvelope(lit.getValue(), tree.getCrs());
                try {
                    if (filterType == SpatialFilterType.CROSSES || !envelopeOnly) {
                        if (filterType == SpatialFilterType.DISJOINT) {
                            reverse = true;
                        }
                        final int[] resultID = tree.searchID(boundFilter);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                        envelopeOnly = false;
                    } else {
                        final int[] resultID = TreeX.search(tree, boundFilter, filterType);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                    }
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", sp.getExpression2());
            }
        } else {
            LOGGER.log(Level.WARNING, "not a spatial operator:{0}", filter.getClass().getName());
        }
    } else {
        LOGGER.finer("Null R-tree in spatial search");
    }

    final LeafReader reader = ctx.reader();
    final BitDocIdSet set = new BitDocIdSet(new FixedBitSet(reader.maxDoc()));
    final DocsEnum termDocs = reader.termDocsEnum(META_FIELD);
    int n = termDocs.nextDoc();
    while (n != DocsEnum.NO_MORE_DOCS) {
        final int docId = termDocs.docID();
        final Document doc = reader.document(docId, ID_FIELDS);
        final String id = doc.get(IDENTIFIER_FIELD_NAME);
        final boolean match = treeMatching.contains(id);
        if (treeSearch && reverse && !match) {
            set.bits().set(docId);

        } else if (!treeSearch || match) {
            if (envelopeOnly && !distanceFilter) {
                set.bits().set(docId);
            } else {
                final Document geoDoc = reader.document(docId, GEOMETRY_FIELDS);
                if (filter.evaluate(geoDoc)) {
                    set.bits().set(docId);
                }
            }
        }
        n = termDocs.nextDoc();
    }

    return set;
}

From source file:org.hibernate.search.spatial.impl.SpatialHashFilter.java

License:LGPL

/**
 * Search the index for document having the correct spatial hash cell id at given grid level.
 *
 * @param context the {@link LeafReaderContext} for which to return the {@link DocIdSet}.
 * @param acceptDocs Bits that represent the allowable docs to match (typically deleted docs but possibly filtering
 * other documents)//from ww  w . ja  v a2  s  .c  om
 * @return a {@link DocIdSet} with the document ids matching
 */
@Override
public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException {
    if (spatialHashCellsIds.size() == 0) {
        return null;
    }

    final LeafReader atomicReader = context.reader();

    BitDocIdSet matchedDocumentsIds = new BitDocIdSet(new FixedBitSet(atomicReader.maxDoc()));
    Boolean found = false;
    for (int i = 0; i < spatialHashCellsIds.size(); i++) {
        Term spatialHashCellTerm = new Term(fieldName, spatialHashCellsIds.get(i));
        DocsEnum spatialHashCellsDocs = atomicReader.termDocsEnum(spatialHashCellTerm);
        if (spatialHashCellsDocs != null) {
            while (true) {
                final int docId = spatialHashCellsDocs.nextDoc();
                if (docId == DocIdSetIterator.NO_MORE_DOCS) {
                    break;
                } else {
                    if (acceptDocs == null || acceptDocs.get(docId)) {
                        matchedDocumentsIds.bits().set(docId);
                        found = true;
                    }
                }
            }
        }
    }

    if (found) {
        return matchedDocumentsIds;
    } else {
        return null;
    }
}

From source file:org.hibernate.search.spatial.impl.SpatialHashQuery.java

License:LGPL

/**
 * Search the index for document having the correct spatial hash cell id at given grid level.
 *
 * @param context the {@link LeafReaderContext} for which to return the {@link DocIdSet}.
 * @return a {@link DocIdSetIterator} with the matching document ids
 *///from  w  w  w.j av  a 2 s  .  com
private DocIdSetIterator createDocIdSetIterator(LeafReaderContext context) throws IOException {
    if (spatialHashCellsIds.size() == 0) {
        return null;
    }

    final LeafReader atomicReader = context.reader();

    BitDocIdSet matchedDocumentsIds = new BitDocIdSet(new FixedBitSet(atomicReader.maxDoc()));
    boolean found = false;
    for (int i = 0; i < spatialHashCellsIds.size(); i++) {
        Term spatialHashCellTerm = new Term(fieldName, spatialHashCellsIds.get(i));
        PostingsEnum spatialHashCellsDocs = atomicReader.postings(spatialHashCellTerm);
        if (spatialHashCellsDocs != null) {
            while (true) {
                final int docId = spatialHashCellsDocs.nextDoc();
                if (docId == DocIdSetIterator.NO_MORE_DOCS) {
                    break;
                } else {
                    matchedDocumentsIds.bits().set(docId);
                    found = true;
                }
            }
        }
    }

    if (found) {
        return matchedDocumentsIds.iterator();
    } else {
        return DocIdSetIterator.empty();
    }
}