search json parsing to allow passing numbers/booleans as strings and not native json types
This commit is contained in:
parent
ef85412f98
commit
6fe329ab69
|
@ -23,7 +23,7 @@ import org.codehaus.jackson.JsonParser;
|
|||
import org.elasticsearch.search.internal.SearchContext;
|
||||
|
||||
/**
|
||||
* @author kimchy (Shay Banon)
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public interface SearchParseElement {
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ import org.elasticsearch.search.SearchParseElement;
|
|||
import org.elasticsearch.search.SearchParseException;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.search.internal.SearchContextFacets;
|
||||
import org.elasticsearch.util.Booleans;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -88,6 +89,8 @@ public class FacetsParseElement implements SearchParseElement {
|
|||
}
|
||||
} else if (token == JsonToken.VALUE_NUMBER_INT) {
|
||||
global = jp.getIntValue() != 0;
|
||||
} else if (token == JsonToken.VALUE_STRING) {
|
||||
global = Booleans.parseBoolean(jp.getText(), global);
|
||||
}
|
||||
}
|
||||
if (facet == null) {
|
||||
|
|
|
@ -23,15 +23,18 @@ import org.codehaus.jackson.JsonParser;
|
|||
import org.codehaus.jackson.JsonToken;
|
||||
import org.elasticsearch.search.SearchParseElement;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.util.Booleans;
|
||||
|
||||
/**
|
||||
* @author kimchy (Shay Banon)
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public class ExplainParseElement implements SearchParseElement {
|
||||
|
||||
@Override public void parse(JsonParser jp, SearchContext context) throws Exception {
|
||||
if (jp.getCurrentToken() == JsonToken.VALUE_NUMBER_INT) {
|
||||
context.explain(jp.getIntValue() != 0);
|
||||
} else if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
|
||||
context.explain(Booleans.parseBoolean(jp.getText(), context.explain()));
|
||||
} else {
|
||||
context.explain(jp.getCurrentToken() == JsonToken.VALUE_TRUE);
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.elasticsearch.search.query;
|
||||
|
||||
import org.codehaus.jackson.JsonParser;
|
||||
import org.codehaus.jackson.JsonToken;
|
||||
import org.elasticsearch.search.SearchParseElement;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
|
||||
|
@ -29,6 +30,10 @@ import org.elasticsearch.search.internal.SearchContext;
|
|||
public class FromParseElement implements SearchParseElement {
|
||||
|
||||
@Override public void parse(JsonParser jp, SearchContext context) throws Exception {
|
||||
context.from(jp.getIntValue());
|
||||
if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
|
||||
context.from(Integer.parseInt(jp.getText()));
|
||||
} else {
|
||||
context.from(jp.getIntValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,11 @@ public class IndicesBoostParseElement implements SearchParseElement {
|
|||
if (indexName.equals(context.shardTarget().index())) {
|
||||
jp.nextToken(); // move to the value
|
||||
// we found our query boost
|
||||
context.queryBoost(jp.getFloatValue());
|
||||
if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
|
||||
context.queryBoost(Float.parseFloat(jp.getText()));
|
||||
} else {
|
||||
context.queryBoost(jp.getFloatValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.elasticsearch.search.query;
|
||||
|
||||
import org.codehaus.jackson.JsonParser;
|
||||
import org.codehaus.jackson.JsonToken;
|
||||
import org.elasticsearch.search.SearchParseElement;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
|
||||
|
@ -29,6 +30,10 @@ import org.elasticsearch.search.internal.SearchContext;
|
|||
public class SizeParseElement implements SearchParseElement {
|
||||
|
||||
@Override public void parse(JsonParser jp, SearchContext context) throws Exception {
|
||||
context.size(jp.getIntValue());
|
||||
if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
|
||||
context.size(Integer.parseInt(jp.getText()));
|
||||
} else {
|
||||
context.size(jp.getIntValue());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@ import org.elasticsearch.index.mapper.FieldMappers;
|
|||
import org.elasticsearch.search.SearchParseElement;
|
||||
import org.elasticsearch.search.SearchParseException;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.util.Booleans;
|
||||
import org.elasticsearch.util.gnu.trove.TObjectIntHashMap;
|
||||
import org.elasticsearch.util.trove.ExtTObjectIntHasMap;
|
||||
|
||||
|
@ -35,7 +36,7 @@ import java.io.IOException;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author kimchy (Shay Banon)
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public class SortParseElement implements SearchParseElement {
|
||||
|
||||
|
@ -87,6 +88,15 @@ public class SortParseElement implements SearchParseElement {
|
|||
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
|
||||
if (token == JsonToken.FIELD_NAME) {
|
||||
innerJsonName = jp.getCurrentName();
|
||||
} else if (token == JsonToken.VALUE_STRING) {
|
||||
if ("type".equals(innerJsonName)) {
|
||||
type = sortFieldTypesMapper.get(jp.getText());
|
||||
if (type == -1) {
|
||||
throw new SearchParseException(context, "No sort type for [" + jp.getText() + "] with field [" + fieldName + "]");
|
||||
}
|
||||
} else if ("reverse".equals(innerJsonName)) {
|
||||
reverse = Booleans.parseBoolean(jp.getText(), reverse);
|
||||
}
|
||||
} else if (token == JsonToken.VALUE_NUMBER_INT) {
|
||||
if ("reverse".equals(innerJsonName)) {
|
||||
reverse = jp.getIntValue() != 0;
|
||||
|
@ -95,13 +105,6 @@ public class SortParseElement implements SearchParseElement {
|
|||
if ("reverse".equals(innerJsonName)) {
|
||||
reverse = true;
|
||||
}
|
||||
} else {
|
||||
if ("type".equals(innerJsonName)) {
|
||||
type = sortFieldTypesMapper.get(jp.getText());
|
||||
if (type == -1) {
|
||||
throw new SearchParseException(context, "No sort type for [" + jp.getText() + "] with field [" + fieldName + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
addSortField(context, sortFields, fieldName, reverse, type);
|
||||
|
|
Loading…
Reference in New Issue