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

org.elasticsearch.action.count.CountRequest Maven / Gradle / Ivy

There is a newer version: 8.15.1
Show newest version
/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.action.count;

import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.Actions;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Required;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.io.FastByteArrayOutputStream;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * A request to count the number of documents matching a specific query. Best created with
 * {@link org.elasticsearch.client.Requests#countRequest(String...)}.
 *
 * 

The request requires the query source to be set either using {@link #query(org.elasticsearch.index.query.QueryBuilder)}, * or {@link #query(byte[])}. * * @author kimchy (shay.banon) * @see CountResponse * @see org.elasticsearch.client.Client#count(CountRequest) * @see org.elasticsearch.client.Requests#countRequest(String...) */ public class CountRequest extends BroadcastOperationRequest { private static final XContentType contentType = Requests.CONTENT_TYPE; public static final float DEFAULT_MIN_SCORE = -1f; private float minScore = DEFAULT_MIN_SCORE; @Nullable protected String queryHint; @Nullable protected String routing; private byte[] querySource; private int querySourceOffset; private int querySourceLength; private boolean querySourceUnsafe; private String[] types = Strings.EMPTY_ARRAY; private String queryParserName; CountRequest() { } /** * Constructs a new count request against the provided indices. No indices provided means it will * run against all indices. */ public CountRequest(String... indices) { super(indices); this.queryHint = null; } @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = super.validate(); if (querySource == null) { validationException = Actions.addValidationError("query is missing", validationException); } return validationException; } public String queryHint() { return queryHint; } /** * Controls the operation threading model. */ @Override public CountRequest operationThreading(BroadcastOperationThreading operationThreading) { super.operationThreading(operationThreading); return this; } @Override protected void beforeLocalFork() { if (querySourceUnsafe) { querySource = Arrays.copyOfRange(querySource, querySourceOffset, querySourceOffset + querySourceLength); querySourceOffset = 0; querySourceUnsafe = false; } } /** * Should the listener be called on a separate thread if needed. */ @Override public CountRequest listenerThreaded(boolean threadedListener) { super.listenerThreaded(threadedListener); return this; } public CountRequest indices(String... indices) { this.indices = indices; return this; } /** * A query hint to optionally later be used when routing the request. */ public CountRequest queryHint(String queryHint) { this.queryHint = queryHint; return this; } /** * The minimum score of the documents to include in the count. */ float minScore() { return minScore; } /** * The minimum score of the documents to include in the count. Defaults to -1 which means all * documents will be included in the count. */ public CountRequest minScore(float minScore) { this.minScore = minScore; return this; } /** * The query source to execute. */ byte[] querySource() { return querySource; } int querySourceOffset() { return querySourceOffset; } int querySourceLength() { return querySourceLength; } /** * The query source to execute. * * @see org.elasticsearch.index.query.xcontent.QueryBuilders */ @Required public CountRequest query(QueryBuilder queryBuilder) { FastByteArrayOutputStream bos = queryBuilder.buildAsUnsafeBytes(); this.querySource = bos.unsafeByteArray(); this.querySourceOffset = 0; this.querySourceLength = bos.size(); this.querySourceUnsafe = true; return this; } /** * The query source to execute in the form of a map. */ @Required public CountRequest query(Map querySource) { try { XContentBuilder builder = XContentFactory.contentBuilder(contentType); builder.map(querySource); return query(builder); } catch (IOException e) { throw new ElasticSearchGenerationException("Failed to generate [" + querySource + "]", e); } } @Required public CountRequest query(XContentBuilder builder) { try { this.querySource = builder.unsafeBytes(); this.querySourceOffset = 0; this.querySourceLength = builder.unsafeBytesLength(); this.querySourceUnsafe = true; return this; } catch (IOException e) { throw new ElasticSearchGenerationException("Failed to generate [" + builder + "]", e); } } /** * The query source to execute. It is preferable to use either {@link #query(byte[])} * or {@link #query(org.elasticsearch.index.query.QueryBuilder)}. */ @Required public CountRequest query(String querySource) { UnicodeUtil.UTF8Result result = Unicode.fromStringAsUtf8(querySource); this.querySource = result.result; this.querySourceOffset = 0; this.querySourceLength = result.length; this.querySourceUnsafe = true; return this; } /** * The query source to execute. */ @Required public CountRequest query(byte[] querySource) { return query(querySource, 0, querySource.length, false); } /** * The query source to execute. */ @Required public CountRequest query(byte[] querySource, int offset, int length, boolean unsafe) { this.querySource = querySource; this.querySourceOffset = offset; this.querySourceLength = length; this.querySourceUnsafe = unsafe; return this; } /** * The query parse name to use. If not set, will use the default one. */ String queryParserName() { return queryParserName; } /** * The query parse name to use. If not set, will use the default one. */ public CountRequest queryParserName(String queryParserName) { this.queryParserName = queryParserName; return this; } /** * The types of documents the query will run against. Defaults to all types. */ String[] types() { return this.types; } /** * The types of documents the query will run against. Defaults to all types. */ public CountRequest types(String... types) { this.types = types; return this; } /** * A comma separated list of routing values to control the shards the search will be executed on. */ public String routing() { return this.routing; } /** * A comma separated list of routing values to control the shards the search will be executed on. */ public CountRequest routing(String routing) { this.routing = routing; return this; } /** * The routing values to control the shards that the search will be executed on. */ public CountRequest routing(String... routings) { this.routing = Strings.arrayToCommaDelimitedString(routings); return this; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); minScore = in.readFloat(); if (in.readBoolean()) { queryHint = in.readUTF(); } if (in.readBoolean()) { routing = in.readUTF(); } querySourceUnsafe = false; querySourceOffset = 0; querySourceLength = in.readVInt(); querySource = new byte[querySourceLength]; in.readFully(querySource); if (in.readBoolean()) { queryParserName = in.readUTF(); } int typesSize = in.readVInt(); if (typesSize > 0) { types = new String[typesSize]; for (int i = 0; i < typesSize; i++) { types[i] = in.readUTF(); } } } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeFloat(minScore); if (queryHint == null) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeUTF(queryHint); } if (routing == null) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeUTF(routing); } out.writeVInt(querySourceLength); out.writeBytes(querySource, querySourceOffset, querySourceLength); if (queryParserName == null) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeUTF(queryParserName); } out.writeVInt(types.length); for (String type : types) { out.writeUTF(type); } } @Override public String toString() { return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + Unicode.fromBytes(querySource) + "]"; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy