Search in sources :

Example 36 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class BytesReference method hashCode.

@Override
public int hashCode() {
    if (hash == null) {
        final BytesRefIterator iterator = iterator();
        BytesRef ref;
        int result = 1;
        try {
            while ((ref = iterator.next()) != null) {
                for (int i = 0; i < ref.length; i++) {
                    result = 31 * result + ref.bytes[ref.offset + i];
                }
            }
        } catch (IOException ex) {
            throw new AssertionError("wont happen", ex);
        }
        return hash = result;
    } else {
        return hash.intValue();
    }
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) IOException(java.io.IOException) BytesRef(org.apache.lucene.util.BytesRef)

Example 37 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class BytesReference method writeTo.

/**
     * Writes the bytes directly to the output stream.
     */
public void writeTo(OutputStream os) throws IOException {
    final BytesRefIterator iterator = iterator();
    BytesRef ref;
    while ((ref = iterator.next()) != null) {
        os.write(ref.bytes, ref.offset, ref.length);
    }
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) BytesRef(org.apache.lucene.util.BytesRef)

Example 38 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class CompositeBytesReference method iterator.

@Override
public BytesRefIterator iterator() {
    if (references.length > 0) {
        return new BytesRefIterator() {

            int index = 0;

            private BytesRefIterator current = references[index++].iterator();

            @Override
            public BytesRef next() throws IOException {
                BytesRef next = current.next();
                if (next == null) {
                    while (index < references.length) {
                        current = references[index++].iterator();
                        next = current.next();
                        if (next != null) {
                            break;
                        }
                    }
                }
                return next;
            }
        };
    } else {
        return () -> null;
    }
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) BytesRef(org.apache.lucene.util.BytesRef)

Example 39 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class PagedBytesReference method iterator.

@Override
public final BytesRefIterator iterator() {
    final int offset = this.offset;
    final int length = this.length;
    // this iteration is page aligned to ensure we do NOT materialize the pages from the ByteArray
    // we calculate the initial fragment size here to ensure that if this reference is a slice we are still page aligned
    // across the entire iteration. The first page is smaller if our offset != 0 then we start in the middle of the page
    // otherwise we iterate full pages until we reach the last chunk which also might end within a page.
    final int initialFragmentSize = offset != 0 ? PAGE_SIZE - (offset % PAGE_SIZE) : PAGE_SIZE;
    return new BytesRefIterator() {

        int position = 0;

        int nextFragmentSize = Math.min(length, initialFragmentSize);

        // this BytesRef is reused across the iteration on purpose - BytesRefIterator interface was designed for this
        final BytesRef slice = new BytesRef();

        @Override
        public BytesRef next() throws IOException {
            if (nextFragmentSize != 0) {
                final boolean materialized = byteArray.get(offset + position, nextFragmentSize, slice);
                assert materialized == false : "iteration should be page aligned but array got materialized";
                position += nextFragmentSize;
                final int remaining = length - position;
                nextFragmentSize = Math.min(remaining, PAGE_SIZE);
                return slice;
            } else {
                assert nextFragmentSize == 0 : "fragmentSize expected [0] but was: [" + nextFragmentSize + "]";
                // we are done with this iteration
                return null;
            }
        }
    };
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) BytesRef(org.apache.lucene.util.BytesRef)

Example 40 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class SimpleQueryParser method newFuzzyQuery.

/**
     * Dispatches to Lucene's SimpleQueryParser's newFuzzyQuery, optionally
     * lowercasing the term first
     */
@Override
public Query newFuzzyQuery(String text, int fuzziness) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setDisableCoord(true);
    for (Map.Entry<String, Float> entry : weights.entrySet()) {
        final String fieldName = entry.getKey();
        try {
            final BytesRef term = getAnalyzer().normalize(fieldName, text);
            Query query = new FuzzyQuery(new Term(fieldName, term), fuzziness);
            bq.add(wrapWithBoost(query, entry.getValue()), BooleanClause.Occur.SHOULD);
        } catch (RuntimeException e) {
            rethrowUnlessLenient(e);
        }
    }
    return super.simplify(bq.build());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesRef (org.apache.lucene.util.BytesRef)1449 Document (org.apache.lucene.document.Document)410 Directory (org.apache.lucene.store.Directory)370 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)266 ArrayList (java.util.ArrayList)186 Test (org.junit.Test)182 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)164 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)152 Term (org.apache.lucene.index.Term)124 Analyzer (org.apache.lucene.analysis.Analyzer)121 IndexReader (org.apache.lucene.index.IndexReader)121 TermsEnum (org.apache.lucene.index.TermsEnum)116 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)110 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)105 IOException (java.io.IOException)104 Field (org.apache.lucene.document.Field)101 StringField (org.apache.lucene.document.StringField)101 CrateUnitTest (io.crate.test.integration.CrateUnitTest)95 TextField (org.apache.lucene.document.TextField)95 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)87