Skip to content

Commit

Permalink
add terms query
Browse files Browse the repository at this point in the history
  • Loading branch information
zshuyi committed Nov 21, 2024
1 parent 3b040af commit d416876
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 7 deletions.
106 changes: 100 additions & 6 deletions server/src/main/java/org/opensearch/action/search/SearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<IndicesOptions.Option> indicesoOptions = EnumSet.noneOf(IndicesOptions.Option.class);
// EnumSet<IndicesOptions.WildcardStates> 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
// );
// }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, TermQueryFieldValue> termQueryFieldValueMap = query.getTermMap();
Set<String> 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
}

}
}

/**
Expand Down

0 comments on commit d416876

Please sign in to comment.