Search in sources :

Example 1 with Queries

use of org.elasticsearch.common.lucene.search.Queries in project elasticsearch by elastic.

the class TermsQueryBuilder method convert.

/**
     * Convert the list in a way that optimizes storage in the case that all
     * elements are either integers or {@link String}s/{@link BytesRef}s. This
     * is useful to help garbage collections for use-cases that involve sending
     * very large terms queries to Elasticsearch. If the list does not only
     * contain integers or {@link String}s, then a list is returned where all
     * {@link String}s have been replaced with {@link BytesRef}s.
     */
static List<?> convert(List<?> list) {
    if (list.isEmpty()) {
        return Collections.emptyList();
    }
    final boolean allNumbers = list.stream().allMatch(o -> o != null && INTEGER_TYPES.contains(o.getClass()));
    if (allNumbers) {
        final long[] elements = list.stream().mapToLong(o -> ((Number) o).longValue()).toArray();
        return new AbstractList<Object>() {

            @Override
            public Object get(int index) {
                return elements[index];
            }

            @Override
            public int size() {
                return elements.length;
            }
        };
    }
    final boolean allStrings = list.stream().allMatch(o -> o != null && STRING_TYPES.contains(o.getClass()));
    if (allStrings) {
        final BytesRefBuilder builder = new BytesRefBuilder();
        try (BytesStreamOutput bytesOut = new BytesStreamOutput()) {
            final int[] endOffsets = new int[list.size()];
            int i = 0;
            for (Object o : list) {
                BytesRef b;
                if (o instanceof BytesRef) {
                    b = (BytesRef) o;
                } else {
                    builder.copyChars(o.toString());
                    b = builder.get();
                }
                bytesOut.writeBytes(b.bytes, b.offset, b.length);
                if (i == 0) {
                    endOffsets[0] = b.length;
                } else {
                    endOffsets[i] = Math.addExact(endOffsets[i - 1], b.length);
                }
                ++i;
            }
            final BytesReference bytes = bytesOut.bytes();
            return new AbstractList<Object>() {

                public Object get(int i) {
                    final int startOffset = i == 0 ? 0 : endOffsets[i - 1];
                    final int endOffset = endOffsets[i];
                    return bytes.slice(startOffset, endOffset - startOffset).toBytesRef();
                }

                public int size() {
                    return endOffsets.length;
                }
            };
        }
    }
    return list.stream().map(o -> o instanceof String ? new BytesRef(o.toString()) : o).collect(Collectors.toList());
}
Also used : Query(org.apache.lucene.search.Query) IntStream(java.util.stream.IntStream) GetResponse(org.elasticsearch.action.get.GetResponse) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) ParsingException(org.elasticsearch.common.ParsingException) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) AbstractList(java.util.AbstractList) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) XContentMapValues(org.elasticsearch.common.xcontent.support.XContentMapValues) GetRequest(org.elasticsearch.action.get.GetRequest) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) BytesRef(org.apache.lucene.util.BytesRef) Client(org.elasticsearch.client.Client) Set(java.util.Set) TermsLookup(org.elasticsearch.indices.TermsLookup) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Collectors(java.util.stream.Collectors) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Objects(java.util.Objects) List(java.util.List) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesRefs(org.elasticsearch.common.lucene.BytesRefs) Queries(org.elasticsearch.common.lucene.search.Queries) Collections(java.util.Collections) AbstractList(java.util.AbstractList) BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

IOException (java.io.IOException)1 AbstractList (java.util.AbstractList)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Query (org.apache.lucene.search.Query)1 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 GetRequest (org.elasticsearch.action.get.GetRequest)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 Client (org.elasticsearch.client.Client)1 ParsingException (org.elasticsearch.common.ParsingException)1 Strings (org.elasticsearch.common.Strings)1