All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polyglotted.pgmodel.search.query.StandardQuery Maven / Gradle / Ivy

Go to download

Standard set of models for geo coding, access control and elastic search abstraction

The newest version!
package io.polyglotted.pgmodel.search.query;

import com.google.common.collect.ImmutableList;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.copyOf;
import static io.polyglotted.pgmodel.util.ModelUtil.jsonEquals;
import static io.polyglotted.pgmodel.search.query.QueryHints.hintsBuilder;
import static java.util.Arrays.asList;

@RequiredArgsConstructor
@ToString(includeFieldNames = false, doNotUseGetters = true)
public final class StandardQuery {
    public final ImmutableList indices;
    public final ImmutableList types;
    public final ImmutableList fields;
    public final ImmutableList expressions;
    public final ImmutableList aggregates;
    public final ImmutableList sorts;
    public final ImmutableList substitutions;
    public final QueryHints queryHints;
    public final Long scrollTimeInMillis;
    public final int offset;
    public final int size;

    @Override
    public boolean equals(Object o) {
        return jsonEquals(this, o);
    }

    @Override
    public int hashCode() {
        return Objects.hash(indices, types, fields, expressions, aggregates, sorts,
           substitutions, queryHints, scrollTimeInMillis, offset, size);
    }

    public static Builder queryBuilder() {
        return new Builder();
    }

    @Setter
    @Accessors(fluent = true, chain = true)
    @NoArgsConstructor(access = AccessLevel.PRIVATE)
    public static class Builder {
        private final List indices = new ArrayList<>();
        private final List types = new ArrayList<>();
        private final List fields = new ArrayList<>();
        private final List expressions = new ArrayList<>();
        private final List aggregates = new ArrayList<>();
        private final List sorts = new ArrayList<>();
        private final List subs = new ArrayList<>();
        private QueryHints hints = hintsBuilder().build();
        private Long scrollTimeInMillis = null;
        private int offset = 0;
        private int size = 10;

        public Builder index(String... indices) {
            this.indices.addAll(asList(indices));
            return this;
        }

        public Builder type(String... types) {
            this.types.addAll(asList(types));
            return this;
        }

        public Builder field(String... fields) {
            this.fields.addAll(asList(fields));
            return this;
        }

        public Builder expression(Expression... expressions) {
            this.expressions.addAll(asList(expressions));
            return this;
        }

        public Builder aggregate(Expression... aggregates) {
            this.aggregates.addAll(asList(aggregates));
            return this;
        }

        public Builder sort(Sort.Builder sortBuilder) {
            return sort(sortBuilder.build());
        }

        public Builder sort(Sort... sorts) {
            this.sorts.addAll(asList(sorts));
            return this;
        }

        public Builder sub(Integer... subs) {
            this.subs.addAll(asList(subs));
            return this;
        }

        public Builder hints(QueryHints.Builder hintBuilder) {
            return hints(hintBuilder.build());
        }

        public Builder hints(QueryHints hints) {
            this.hints = hints;
            return this;
        }

        public StandardQuery build() {
            return new StandardQuery(copyOf(indices), copyOf(types), copyOf(fields), copyOf(expressions),
               copyOf(aggregates), copyOf(sorts), copyOf(subs), checkNotNull(hints), scrollTimeInMillis, offset, size);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy