Skip to content

Commit

Permalink
Fixup after @javanna's review
Browse files Browse the repository at this point in the history
  • Loading branch information
imotov committed Mar 31, 2015
1 parent 882c23a commit 802b826
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 123 deletions.
Expand Up @@ -37,12 +37,12 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeStringArrayValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeStringValue;

/**
* A multi percolate request that encapsulates multiple {@link PercolateRequest} instances in a single api call.
Expand Down Expand Up @@ -175,39 +175,35 @@ public List<? extends IndicesRequest> subRequests() {
private void parsePercolateAction(XContentParser parser, PercolateRequest percolateRequest, boolean allowExplicitIndex) throws IOException {
String globalIndex = indices != null && indices.length > 0 ? indices[0] : null;

Map<String, Object> header = parseToMap(parser);
Map<String, Object> header = parser.map();

if (header.containsKey("id")) {
GetRequest getRequest = new GetRequest(globalIndex);
percolateRequest.getRequest(getRequest);
for (Map.Entry<String, Object> entry : header.entrySet()) {
Object value = entry.getValue();
if ("id".equals(entry.getKey())) {
getRequest.id((String) value);
getRequest.id(nodeStringValue(value, null));
header.put("id", entry.getValue());
} else if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
if (!allowExplicitIndex) {
throw new ElasticsearchIllegalArgumentException("explicit index in multi percolate is not allowed");
}
getRequest.index((String) value);
getRequest.index(nodeStringValue(value, null));
} else if ("type".equals(entry.getKey())) {
getRequest.type((String) value);
getRequest.type(nodeStringValue(value, null));
} else if ("preference".equals(entry.getKey())) {
getRequest.preference((String) value);
getRequest.preference(nodeStringValue(value, null));
} else if ("routing".equals(entry.getKey())) {
getRequest.routing((String) value);
getRequest.routing(nodeStringValue(value, null));
} else if ("percolate_index".equals(entry.getKey()) || "percolate_indices".equals(entry.getKey()) || "percolateIndex".equals(entry.getKey()) || "percolateIndices".equals(entry.getKey())) {
if (value instanceof String[]) {
percolateRequest.indices((String[]) value);
} else {
percolateRequest.indices(Strings.splitStringByCommaToArray((String) value));
}
percolateRequest.indices(nodeStringArrayValue(value));
} else if ("percolate_type".equals(entry.getKey()) || "percolateType".equals(entry.getKey())) {
percolateRequest.documentType((String) value);
percolateRequest.documentType(nodeStringValue(value, null));
} else if ("percolate_preference".equals(entry.getKey()) || "percolatePreference".equals(entry.getKey())) {
percolateRequest.preference((String) value);
percolateRequest.preference(nodeStringValue(value, null));
} else if ("percolate_routing".equals(entry.getKey()) || "percolateRouting".equals(entry.getKey())) {
percolateRequest.routing((String) value);
percolateRequest.routing(nodeStringValue(value, null));
}
}

Expand All @@ -231,49 +227,19 @@ private void parsePercolateAction(XContentParser parser, PercolateRequest percol
if (!allowExplicitIndex) {
throw new ElasticsearchIllegalArgumentException("explicit index in multi percolate is not allowed");
}
if (value instanceof String[]) {
percolateRequest.indices((String[]) value);
} else {
percolateRequest.indices(Strings.splitStringByCommaToArray((String) value));
}
percolateRequest.indices(nodeStringArrayValue(value));
} else if ("type".equals(entry.getKey())) {
percolateRequest.documentType((String) value);
percolateRequest.documentType(nodeStringValue(value, null));
} else if ("preference".equals(entry.getKey())) {
percolateRequest.preference((String) value);
percolateRequest.preference(nodeStringValue(value, null));
} else if ("routing".equals(entry.getKey())) {
percolateRequest.routing((String) value);
percolateRequest.routing(nodeStringValue(value, null));
}
}
}
percolateRequest.indicesOptions(IndicesOptions.fromMap(header, indicesOptions));
}

private Map<String, Object> parseToMap(XContentParser parser) throws IOException {
Map<String, Object> header = new HashMap<>();

String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
header.put(currentFieldName, parser.text());
} else if (token == XContentParser.Token.START_ARRAY) {
header.put(currentFieldName, parseArray(parser));
}
}
return header;
}

private String[] parseArray(XContentParser parser) throws IOException {
final List<String> list = new ArrayList<>();
assert parser.currentToken() == XContentParser.Token.START_ARRAY;
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
list.add(parser.text());
}
return list.toArray(new String[list.size()]);
}

private int findNextMarker(byte marker, int from, BytesReference data, int length) {
for (int i = from; i < length; i++) {
if (data.get(i) == marker) {
Expand Down
Expand Up @@ -40,8 +40,12 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeStringArrayValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeStringValue;

/**
* A multi search API request.
Expand Down Expand Up @@ -111,82 +115,35 @@ public MultiSearchRequest add(BytesReference data, boolean contentUnsafe, @Nulla
searchRequest.searchType(searchType);

IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
boolean ignoreUnavailable = defaultOptions.ignoreUnavailable();
boolean allowNoIndices = defaultOptions.allowNoIndices();
boolean expandWildcardsOpen = defaultOptions.expandWildcardsOpen();
boolean expandWildcardsClosed = defaultOptions.expandWildcardsClosed();


// now parse the action
if (nextMarker - from > 0) {
try (XContentParser parser = xContent.createParser(data.slice(from, nextMarker - from))) {
// Move to START_OBJECT, if token is null, its an empty data
XContentParser.Token token = parser.nextToken();
if (token != null) {
assert token == XContentParser.Token.START_OBJECT;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("index".equals(currentFieldName) || "indices".equals(currentFieldName)) {
if (!allowExplicitIndex) {
throw new ElasticsearchIllegalArgumentException("explicit index in multi search is not allowed");
}
searchRequest.indices(Strings.splitStringByCommaToArray(parser.text()));
} else if ("type".equals(currentFieldName) || "types".equals(currentFieldName)) {
searchRequest.types(Strings.splitStringByCommaToArray(parser.text()));
} else if ("search_type".equals(currentFieldName) || "searchType".equals(currentFieldName)) {
searchRequest.searchType(parser.text());
} else if ("query_cache".equals(currentFieldName) || "queryCache".equals(currentFieldName)) {
searchRequest.queryCache(parser.booleanValue());
} else if ("preference".equals(currentFieldName)) {
searchRequest.preference(parser.text());
} else if ("routing".equals(currentFieldName)) {
searchRequest.routing(parser.text());
} else if ("ignore_unavailable".equals(currentFieldName) || "ignoreUnavailable".equals(currentFieldName)) {
ignoreUnavailable = parser.booleanValue();
} else if ("allow_no_indices".equals(currentFieldName) || "allowNoIndices".equals(currentFieldName)) {
allowNoIndices = parser.booleanValue();
} else if ("expand_wildcards".equals(currentFieldName) || "expandWildcards".equals(currentFieldName)) {
String[] wildcards = Strings.splitStringByCommaToArray(parser.text());
for (String wildcard : wildcards) {
if ("open".equals(wildcard)) {
expandWildcardsOpen = true;
} else if ("closed".equals(wildcard)) {
expandWildcardsClosed = true;
} else {
throw new ElasticsearchIllegalArgumentException("No valid expand wildcard value [" + wildcard + "]");
}
}
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("index".equals(currentFieldName) || "indices".equals(currentFieldName)) {
if (!allowExplicitIndex) {
throw new ElasticsearchIllegalArgumentException("explicit index in multi search is not allowed");
}
searchRequest.indices(parseArray(parser));
} else if ("type".equals(currentFieldName) || "types".equals(currentFieldName)) {
searchRequest.types(parseArray(parser));
} else if ("expand_wildcards".equals(currentFieldName) || "expandWildcards".equals(currentFieldName)) {
String[] wildcards = parseArray(parser);
for (String wildcard : wildcards) {
if ("open".equals(wildcard)) {
expandWildcardsOpen = true;
} else if ("closed".equals(wildcard)) {
expandWildcardsClosed = true;
} else {
throw new ElasticsearchIllegalArgumentException("No valid expand wildcard value [" + wildcard + "]");
}
}
} else {
throw new ElasticsearchParseException(currentFieldName + " doesn't support arrays");
}
Map<String, Object> source = parser.map();
for (Map.Entry<String, Object> entry : source.entrySet()) {
Object value = entry.getValue();
if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
if (!allowExplicitIndex) {
throw new ElasticsearchIllegalArgumentException("explicit index in multi percolate is not allowed");
}
searchRequest.indices(nodeStringArrayValue(value));
} else if ("type".equals(entry.getKey()) || "types".equals(entry.getKey())) {
searchRequest.types(nodeStringArrayValue(value));
} else if ("search_type".equals(entry.getKey()) || "searchType".equals(entry.getKey())) {
searchRequest.searchType(nodeStringValue(value, null));
} else if ("query_cache".equals(entry.getKey()) || "queryCache".equals(entry.getKey())) {
searchRequest.queryCache(nodeBooleanValue(value));
} else if ("preference".equals(entry.getKey())) {
searchRequest.preference(nodeStringValue(value, null));
} else if ("routing".equals(entry.getKey())) {
searchRequest.routing(nodeStringValue(value, null));
}
}
defaultOptions = IndicesOptions.fromMap(source, defaultOptions);
}
}
searchRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed, defaultOptions));
searchRequest.indicesOptions(defaultOptions);

// move pointers
from = nextMarker + 1;
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;

import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeStringArrayValue;

/**
* Controls how to deal with unavailable concrete indices (closed or missing), how wildcard expressions are expanded
Expand Down Expand Up @@ -165,12 +166,7 @@ public static IndicesOptions fromParameters(Object wildcardsString, Object ignor
expandWildcardsOpen = defaultSettings.expandWildcardsOpen();
expandWildcardsClosed = defaultSettings.expandWildcardsClosed();
} else {
String[] wildcards;
if (wildcardsString instanceof String[]) {
wildcards = (String[]) wildcardsString;
} else {
wildcards = Strings.splitStringByCommaToArray((String) wildcardsString);
}
String[] wildcards = nodeStringArrayValue(wildcardsString);
for (String wildcard : wildcards) {
if ("open".equals(wildcard)) {
expandWildcardsOpen = true;
Expand Down
Expand Up @@ -387,4 +387,23 @@ public static Map<String, Object> nodeMapValue(Object node, String desc) {
throw new ElasticsearchParseException(desc + " should be a hash but was of type: " + node.getClass());
}
}

/**
* Returns an array of string value from a node value.
*
* If the node represents an array the corresponding array of strings is returned.
* Otherwise the node is treated as a comma-separated string.
*/
public static String[] nodeStringArrayValue(Object node) {
if (isArray(node)) {
List list = (List) node;
String[] arr = new String[list.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = nodeStringValue(list.get(i), null);
}
return arr;
} else {
return Strings.splitStringByCommaToArray(node.toString());
}
}
}

0 comments on commit 802b826

Please sign in to comment.