List of usage examples for org.apache.lucene.util BitSet get
boolean get(int index);
index. From source file:com.dasasian.chok.lucene.LuceneServer.java
License:Apache License
/** * Merges the already sorted sub-lists to one big sorted list. *///w ww . j av a2s . co m private static List<Hit> mergeFieldSort(FieldSortComparator comparator, int count, ScoreDoc[][] sortedFieldDocs, String[] shards, String nodeName) { int[] arrayPositions = new int[sortedFieldDocs.length]; final List<Hit> sortedResult = new ArrayList<>(count); BitSet listDone = new BitSet(sortedFieldDocs.length); for (int subListIndex = 0; subListIndex < arrayPositions.length; subListIndex++) { if (sortedFieldDocs[subListIndex].length == 0) { listDone.set(subListIndex, true); } } do { int fieldDocArrayWithSmallestFieldDoc = -1; FieldDoc smallestFieldDoc = null; for (int subListIndex = 0; subListIndex < arrayPositions.length; subListIndex++) { if (!listDone.get(subListIndex)) { FieldDoc hit = (FieldDoc) sortedFieldDocs[subListIndex][arrayPositions[subListIndex]]; if (smallestFieldDoc == null || comparator.compare(hit.fields, smallestFieldDoc.fields) < 0) { smallestFieldDoc = hit; fieldDocArrayWithSmallestFieldDoc = subListIndex; } } } ScoreDoc[] smallestElementList = sortedFieldDocs[fieldDocArrayWithSmallestFieldDoc]; FieldDoc fieldDoc = (FieldDoc) smallestElementList[arrayPositions[fieldDocArrayWithSmallestFieldDoc]]; arrayPositions[fieldDocArrayWithSmallestFieldDoc]++; final Hit hit = new Hit(shards[fieldDocArrayWithSmallestFieldDoc], nodeName, fieldDoc.score, fieldDoc.doc); hit.setSortFields(WritableType.convertComparable(comparator.getFieldTypes(), fieldDoc.fields)); sortedResult.add(hit); if (arrayPositions[fieldDocArrayWithSmallestFieldDoc] >= smallestElementList.length) { listDone.set(fieldDocArrayWithSmallestFieldDoc, true); } } while (sortedResult.size() < count && listDone.cardinality() < arrayPositions.length); return sortedResult; }
From source file:com.dasasian.chok.lucene.LuceneServer.java
License:Apache License
/** * Search in the given shards and return max hits for given query * * @param query the query/*from w w w . java 2 s . c om*/ * @param freqs document frequency writer * @param shards the shards * @param result the writable for the result * @param max max results * @param sort the sort order * @param timeout timeout value * @param filter filter to apply * @throws IOException when an error occurs */ protected final void search(final Query query, final DocumentFrequencyWritable freqs, final String[] shards, final HitsMapWritable result, final int max, Sort sort, long timeout, Filter filter) throws IOException { timeout = getCollectorTimeout(timeout); final Query rewrittenQuery = rewrite(query, shards); final int numDocs = freqs.getNumDocsAsInteger(); final Weight weight = rewrittenQuery .weight(new CachedDfSource(freqs.getAll(), numDocs, new DefaultSimilarity())); int totalHits = 0; final int shardsCount = shards.length; // Run the search in parallel on the shards with a thread pool. CompletionService<SearchResult> csSearch = new ExecutorCompletionService<>(threadPool); for (int i = 0; i < shardsCount; i++) { SearchCall call = new SearchCall(shards[i], weight, max, sort, timeout, i, filter); csSearch.submit(call); } final ScoreDoc[][] scoreDocs = new ScoreDoc[shardsCount][]; ScoreDoc scoreDocExample = null; for (int i = 0; i < shardsCount; i++) { try { final SearchResult searchResult = csSearch.take().get(); final int callIndex = searchResult.getSearchCallIndex(); totalHits += searchResult._totalHits; scoreDocs[callIndex] = searchResult._scoreDocs; if (scoreDocExample == null && scoreDocs[callIndex].length > 0) { scoreDocExample = scoreDocs[callIndex][0]; } } catch (InterruptedException e) { throw new IOException("Multithread shard search interrupted:", e); } catch (ExecutionException e) { throw new IOException("Multithread shard search could not be executed:", e); } } result.addTotalHits(totalHits); final Iterable<Hit> finalHitList; // Limit the request to the number requested or the total number of // documents, whichever is smaller. int limit = Math.min(numDocs, max); if (sort == null || totalHits == 0) { final ChokHitQueue hq = new ChokHitQueue(limit); int pos = 0; BitSet done = new BitSet(shardsCount); while (done.cardinality() != shardsCount) { ScoreDoc scoreDoc = null; for (int i = 0; i < shardsCount; i++) { // only process this shard if it is not yet done. if (!done.get(i)) { final ScoreDoc[] docs = scoreDocs[i]; if (pos < docs.length) { scoreDoc = docs[pos]; final Hit hit = new Hit(shards[i], getNodeName(), scoreDoc.score, scoreDoc.doc); if (!hq.insert(hit)) { // no doc left that has a higher score than the lowest score in // the queue done.set(i, true); } } else { // no docs left in this shard done.set(i, true); } } } // we always wait until we got all hits from this position in all // shards. pos++; if (scoreDoc == null) { // we do not have any more data break; } } finalHitList = hq; } else { WritableType[] sortFieldsTypes; FieldDoc fieldDoc = (FieldDoc) scoreDocExample; sortFieldsTypes = WritableType.detectWritableTypes(fieldDoc.fields); result.setSortFieldTypes(sortFieldsTypes); finalHitList = mergeFieldSort(new FieldSortComparator(sort.getSort(), sortFieldsTypes), limit, scoreDocs, shards, getNodeName()); } for (Hit hit : finalHitList) { if (hit != null) { result.addHit(hit); } } }
From source file:de.unihildesheim.iw.lucene.util.BitsUtilsTest.java
License:Open Source License
@SuppressWarnings("ImplicitNumericConversion") @Test/* w ww .j ava 2s . c o m*/ public void testBits2BitSet() throws Exception { final FixedBitSet fbs = new FixedBitSet(11); fbs.set(1); fbs.set(3); fbs.set(6); fbs.set(7); fbs.set(8); fbs.set(10); final BitSet result = BitsUtils.bits2BitSet(fbs); Assert.assertEquals("Bit count mismatch.", fbs.cardinality(), result.cardinality()); for (int i = 0; i < 11; i++) { Assert.assertEquals("Bits mismatch.", fbs.get(i), result.get(i)); } }
From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtils.java
License:Open Source License
/** * Get the highest document id stored in the {@link DocIdSet}. * * @param dis DocIdSet/*from www .j a va2 s. c o m*/ * @return Highest document number or {@code -1}, if there's no document * @throws IOException Thrown on low-level i/o-errors */ public static int maxDoc(@NotNull final DocIdSet dis) throws IOException { final int maxDoc; @Nullable final DocIdSetIterator disi = dis.iterator(); if (disi == null) { maxDoc = 0; } else { @Nullable BitSet bitSet; bitSet = BitSetIterator.getFixedBitSetOrNull(disi); if (bitSet == null) { bitSet = BitSetIterator.getSparseFixedBitSetOrNull(disi); } if (bitSet == null) { bitSet = BitsUtils.bits2BitSet(dis.bits()); } if (bitSet == null) { maxDoc = StreamUtils.stream(dis).sorted().max().getAsInt(); } else { if (bitSet.length() == 0) { maxDoc = -1; } else if (bitSet.length() == 1) { maxDoc = bitSet.get(0) ? 0 : -1; } else { maxDoc = bitSet.prevSetBit(bitSet.length() - 1); } } } return maxDoc; }
From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtilsTest.java
License:Open Source License
@Test public void testBits() throws Exception { final DocIdSet dis = new Builder(9).add(1).add(3).add(6).add(7).add(8).build(); final BitSet bits = DocIdSetUtils.bits(dis); Assert.assertNotNull("Bits were null.", bits); final boolean allMatched = bits.get(1) && bits.get(3) && bits.get(6) && bits.get(7) && bits.get(8); Assert.assertTrue("Not all required bits set.", allMatched); final boolean failMatched = bits.get(2) || bits.get(4) || bits.get(5); Assert.assertFalse("Wrong bits set.", failMatched); }
From source file:org.codelibs.elasticsearch.search.MultiValueMode.java
License:Apache License
/** * Return a {NumericDocValues} instance that can be used to sort root documents * with this mode, the provided values and filters for root/inner documents. * * For every root document, the values of its inner documents will be aggregated. * If none of the inner documents has a value, then <code>missingValue</code> is returned. * * Allowed Modes: SUM, AVG, MIN, MAX/* w w w.java2 s .co m*/ * * NOTE: Calling the returned instance on docs that are not root docs is illegal * The returned instance can only be evaluate the current and upcoming docs */ public NumericDocValues select(final SortedNumericDocValues values, final long missingValue, final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException { if (rootDocs == null || innerDocs == null) { return select(DocValues.emptySortedNumeric(maxDoc), missingValue); } return new NumericDocValues() { int lastSeenRootDoc = 0; long lastEmittedValue = missingValue; @Override public long get(int rootDoc) { assert rootDocs.get(rootDoc) : "can only sort root documents"; assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs"; if (rootDoc == lastSeenRootDoc) { return lastEmittedValue; } try { final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1); final int firstNestedDoc; if (innerDocs.docID() > prevRootDoc) { firstNestedDoc = innerDocs.docID(); } else { firstNestedDoc = innerDocs.advance(prevRootDoc + 1); } lastSeenRootDoc = rootDoc; lastEmittedValue = pick(values, missingValue, innerDocs, firstNestedDoc, rootDoc); return lastEmittedValue; } catch (IOException e) { throw new RuntimeException(e); } } }; }
From source file:org.codelibs.elasticsearch.search.MultiValueMode.java
License:Apache License
/** * Return a {NumericDoubleValues} instance that can be used to sort root documents * with this mode, the provided values and filters for root/inner documents. * * For every root document, the values of its inner documents will be aggregated. * If none of the inner documents has a value, then <code>missingValue</code> is returned. * * Allowed Modes: SUM, AVG, MIN, MAX//w w w.j av a 2s . c o m * * NOTE: Calling the returned instance on docs that are not root docs is illegal * The returned instance can only be evaluate the current and upcoming docs */ public NumericDoubleValues select(final SortedNumericDoubleValues values, final double missingValue, final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException { if (rootDocs == null || innerDocs == null) { return select(FieldData.emptySortedNumericDoubles(maxDoc), missingValue); } return new NumericDoubleValues() { int lastSeenRootDoc = 0; double lastEmittedValue = missingValue; @Override public double get(int rootDoc) { assert rootDocs.get(rootDoc) : "can only sort root documents"; assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs"; if (rootDoc == lastSeenRootDoc) { return lastEmittedValue; } try { final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1); final int firstNestedDoc; if (innerDocs.docID() > prevRootDoc) { firstNestedDoc = innerDocs.docID(); } else { firstNestedDoc = innerDocs.advance(prevRootDoc + 1); } lastSeenRootDoc = rootDoc; lastEmittedValue = pick(values, missingValue, innerDocs, firstNestedDoc, rootDoc); return lastEmittedValue; } catch (IOException e) { throw new RuntimeException(e); } } }; }
From source file:org.codelibs.elasticsearch.search.MultiValueMode.java
License:Apache License
/** * Return a {BinaryDocValues} instance that can be used to sort root documents * with this mode, the provided values and filters for root/inner documents. * * For every root document, the values of its inner documents will be aggregated. * If none of the inner documents has a value, then <code>missingValue</code> is returned. * * Allowed Modes: MIN, MAX/*from w ww. ja v a 2 s . c om*/ * * NOTE: Calling the returned instance on docs that are not root docs is illegal * The returned instance can only be evaluate the current and upcoming docs */ public BinaryDocValues select(final SortedBinaryDocValues values, final BytesRef missingValue, final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException { if (rootDocs == null || innerDocs == null) { return select(FieldData.emptySortedBinary(maxDoc), missingValue); } final BinaryDocValues selectedValues = select(values, null); return new BinaryDocValues() { final BytesRefBuilder builder = new BytesRefBuilder(); int lastSeenRootDoc = 0; BytesRef lastEmittedValue = missingValue; @Override public BytesRef get(int rootDoc) { assert rootDocs.get(rootDoc) : "can only sort root documents"; assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs"; if (rootDoc == lastSeenRootDoc) { return lastEmittedValue; } try { final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1); final int firstNestedDoc; if (innerDocs.docID() > prevRootDoc) { firstNestedDoc = innerDocs.docID(); } else { firstNestedDoc = innerDocs.advance(prevRootDoc + 1); } lastSeenRootDoc = rootDoc; lastEmittedValue = pick(selectedValues, builder, innerDocs, firstNestedDoc, rootDoc); if (lastEmittedValue == null) { lastEmittedValue = missingValue; } return lastEmittedValue; } catch (IOException e) { throw new RuntimeException(e); } } }; }
From source file:org.codelibs.elasticsearch.search.MultiValueMode.java
License:Apache License
/** * Return a {SortedDocValues} instance that can be used to sort root documents * with this mode, the provided values and filters for root/inner documents. * * For every root document, the values of its inner documents will be aggregated. * * Allowed Modes: MIN, MAX/*from w w w.jav a2s . com*/ * * NOTE: Calling the returned instance on docs that are not root docs is illegal * The returned instance can only be evaluate the current and upcoming docs */ public SortedDocValues select(final RandomAccessOrds values, final BitSet rootDocs, final DocIdSetIterator innerDocs) throws IOException { if (rootDocs == null || innerDocs == null) { return select(DocValues.emptySortedSet()); } final SortedDocValues selectedValues = select(values); return new SortedDocValues() { int lastSeenRootDoc = 0; int lastEmittedOrd = -1; @Override public BytesRef lookupOrd(int ord) { return selectedValues.lookupOrd(ord); } @Override public int getValueCount() { return selectedValues.getValueCount(); } @Override public int getOrd(int rootDoc) { assert rootDocs.get(rootDoc) : "can only sort root documents"; assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs"; if (rootDoc == lastSeenRootDoc) { return lastEmittedOrd; } try { final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1); final int firstNestedDoc; if (innerDocs.docID() > prevRootDoc) { firstNestedDoc = innerDocs.docID(); } else { firstNestedDoc = innerDocs.advance(prevRootDoc + 1); } lastSeenRootDoc = rootDoc; return lastEmittedOrd = pick(selectedValues, innerDocs, firstNestedDoc, rootDoc); } catch (IOException e) { throw new RuntimeException(e); } } }; }
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 w w . ja v a 2s . 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; }