From d4168762ad4d13dc9660ccf3778f2afbc464ffbb Mon Sep 17 00:00:00 2001 From: amberzsy Date: Thu, 21 Nov 2024 17:27:28 +0000 Subject: [PATCH] add terms query --- .../action/search/SearchRequest.java | 106 +++++++++++++++++- .../search/builder/SearchSourceBuilder.java | 36 +++++- 2 files changed, 135 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/opensearch/action/search/SearchRequest.java b/server/src/main/java/org/opensearch/action/search/SearchRequest.java index b1b6f61dc2326..7ec9c49ebac18 100644 --- a/server/src/main/java/org/opensearch/action/search/SearchRequest.java +++ b/server/src/main/java/org/opensearch/action/search/SearchRequest.java @@ -51,12 +51,10 @@ import org.opensearch.search.builder.PointInTimeBuilder; import org.opensearch.search.builder.SearchSourceBuilder; import org.opensearch.search.internal.SearchContext; +import static org.opensearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM; import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.Map; -import java.util.Objects; +import java.util.*; import static org.opensearch.action.ValidateActions.addValidationError; @@ -165,8 +163,104 @@ public SearchRequest(String[] indices, SearchSourceBuilder source) { public SearchRequest(opensearch.proto.SearchRequest proto) { this(); source = new SearchSourceBuilder(proto); - searchType = SearchType.QUERY_THEN_FETCH; - batchedReduceSize = DEFAULT_BATCHED_REDUCE_SIZE; +// searchType = SearchType.QUERY_THEN_FETCH; +// batchedReduceSize = DEFAULT_BATCHED_REDUCE_SIZE; +// if (source != null) { +// source = new SearchSourceBuilder(); +// } + if (proto.hasPreFilterShardSize()) { + batchedReduceSize = proto.getBatchedReduceSize(); + } + if (proto.hasMaxConcurrentShardRequests()) { + maxConcurrentShardRequests = proto.getMaxConcurrentShardRequests(); + } + if(proto.hasAllowPartialSearchResults()) { + allowPartialSearchResults = proto.getAllowPartialSearchResults(); + } else { + allowPartialSearchResults = false; + } + if (proto.hasPhaseTook()) { + phaseTook = proto.getPhaseTook(); + } else { + phaseTook = true; + } + switch (proto.getSearchType()) { + case SEARCH_TYPE_QUERY_THEN_FETCH: + searchType = SearchType.QUERY_THEN_FETCH; + break; + case SEARCH_TYPE_DFS_QUERY_THEN_FETCH: + searchType = SearchType.DFS_QUERY_THEN_FETCH; + break; + default: + searchType = SearchType.DEFAULT; + } + if (proto.hasRequestCache()) { + requestCache = proto.getRequestCache(); + } +// routing = String.join(",", proto.getRoutingList()); + preference = proto.getPreference(); +// source = new SearchSourceBuilder(proto); +// EnumSet indicesoOptions = EnumSet.noneOf(IndicesOptions.Option.class); +// EnumSet wildcardStates = EnumSet.noneOf(IndicesOptions.WildcardStates.class); +// +// if (!proto.hasAllowNoIndices()) { // add option by default +// indicesoOptions.add(IndicesOptions.Option.ALLOW_NO_INDICES); +// } else if (proto.getAllowNoIndices()) { +// indicesoOptions.add(IndicesOptions.Option.ALLOW_NO_INDICES); +// } +// +// if (proto.getIgnoreUnavailable()) { +// indicesoOptions.add(IndicesOptions.Option.IGNORE_UNAVAILABLE); +// } +// +// if (!proto.hasIgnoreThrottled()) { // add option by default +// indicesoOptions.add(IndicesOptions.Option.IGNORE_THROTTLED); +// } else if (proto.getIgnoreThrottled()) { +// indicesoOptions.add(IndicesOptions.Option.IGNORE_THROTTLED); +// } +// +// for (opensearch.proto.SearchRequest.ExpandWildcard wc : proto.getExpandWildcardsList()) { +// switch (wc) { +// case EXPAND_WILDCARD_OPEN: +// wildcardStates.add(IndicesOptions.WildcardStates.OPEN); +// break; +// case EXPAND_WILDCARD_CLOSED: +// wildcardStates.add(IndicesOptions.WildcardStates.CLOSED); +// break; +// case EXPAND_WILDCARD_HIDDEN: +// wildcardStates.add(IndicesOptions.WildcardStates.HIDDEN); +// break; +// case EXPAND_WILDCARD_NONE: +// wildcardStates.clear(); +// break; +// case EXPAND_WILDCARD_ALL: +// wildcardStates.addAll(EnumSet.allOf(IndicesOptions.WildcardStates.class)); +// break; +// } +// } +// indicesOptions = new IndicesOptions(indicesoOptions, wildcardStates); +// if (proto.hasSearchPipeline()) { +// pipeline = proto.getSearchPipeline(); +// } +// allowPartialSearchResults = proto.getAllowPartialSearchResults(); +// if (proto.hasCancelAfterTimeInterval()) { +// TimeValue cancelAfter = new TimeValue(Long.parseLong(proto.getCancelAfterTimeInterval())); +// cancelAfterTimeInterval = cancelAfter; +// } +// ccsMinimizeRoundtrips = proto.getCcsMinimizeRoundtrips(); +// boolean totalHitsAsInt = proto.getRestTotalHitsAsInt(); +// Integer trackTotalHitsUpTo = source().trackTotalHitsUpTo(); +// if (trackTotalHitsUpTo == null) { +// source().trackTotalHits(true); +// } else if (trackTotalHitsUpTo != SearchContext.TRACK_TOTAL_HITS_ACCURATE && trackTotalHitsUpTo != SearchContext.TRACK_TOTAL_HITS_DISABLED) { +// throw new IllegalArgumentException( +// "[" +// + TOTAL_HITS_AS_INT_PARAM +// + "] cannot be used " +// + "if the tracking of total hits is not accurate, got " +// + trackTotalHitsUpTo +// ); +// } } /** diff --git a/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java b/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java index 3741d25d3679e..d20bfd026dc49 100644 --- a/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java +++ b/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java @@ -32,6 +32,7 @@ package org.opensearch.search.builder; +import opensearch.proto.GeneralNumber; import opensearch.proto.QueryContainer; import opensearch.proto.TermQuery; import opensearch.proto.TermQueryFieldValue; @@ -388,8 +389,41 @@ public void writeTo(StreamOutput out) throws IOException { public SearchSourceBuilder(opensearch.proto.SearchRequest proto) { this(); - queryBuilder = QueryBuilders.matchAllQuery(); + QueryContainer query = proto.getRequestBody().getQuery(); + if (query.hasMatchAll()) { + queryBuilder = QueryBuilders.matchAllQuery(); + } + if (!query.getTermMap().isEmpty()) { + System.out.println("============ HERE"); + Map termQueryFieldValueMap = query.getTermMap(); + Set fields = termQueryFieldValueMap.keySet(); + String fieldName = fields.stream().findFirst().get(); + TermQueryFieldValue termQueryFieldValue = termQueryFieldValueMap.get(fieldName); + if (termQueryFieldValue.hasTermQuery()) { + TermQuery termQuery = termQueryFieldValue.getTermQuery(); + if (termQuery.getValue().hasBoolValue()) { + queryBuilder = QueryBuilders.termQuery(fieldName, termQuery.getValue().getBoolValue()); + } else if (termQuery.getValue().hasStringValue()) { + System.out.println("fieldName: " + fieldName + " ======= VALUE ==== " + termQuery.getValue().getStringValue().getValue()); + queryBuilder = QueryBuilders.termQuery(fieldName, termQuery.getValue().getStringValue().getValue()); + } else if (termQuery.getValue().hasGeneralNumber()) { + GeneralNumber valueNumber = termQuery.getValue().getGeneralNumber(); + if (valueNumber.hasDoubleValue()) { + queryBuilder = QueryBuilders.termQuery(fieldName, termQuery.getValue().getGeneralNumber().getDoubleValue()); + } else if (valueNumber.hasInt32Value()) { + queryBuilder = QueryBuilders.termQuery(fieldName, termQuery.getValue().getGeneralNumber().getInt32Value()); + } else if (valueNumber.hasInt64Value()) { + queryBuilder = QueryBuilders.termQuery(fieldName, termQuery.getValue().getGeneralNumber().getInt64Value()); + } else if (valueNumber.hasFloatValue()) { + queryBuilder = QueryBuilders.termQuery(fieldName, termQuery.getValue().getGeneralNumber().getFloatValue()); + } + } + } else if (termQueryFieldValue.hasFieldValue()) { + // TODO + } + + } } /**