Skip to content

Commit

Permalink
search json parsing to allow passing numbers/booleans as strings and …
Browse files Browse the repository at this point in the history
…not native json types
  • Loading branch information
kimchy committed Mar 12, 2010
1 parent ef85412 commit 6fe329a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.elasticsearch.search.internal.SearchContext;

/**
* @author kimchy (Shay Banon)
* @author kimchy (shay.banon)
*/
public interface SearchParseElement {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +30,10 @@
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +30,10 @@
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
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;

import java.io.IOException;
import java.util.List;

/**
* @author kimchy (Shay Banon)
* @author kimchy (shay.banon)
*/
public class SortParseElement implements SearchParseElement {

Expand Down Expand Up @@ -87,6 +88,15 @@ private void addCompoundSortField(JsonParser jp, SearchContext context, List<Sor
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;
Expand All @@ -95,13 +105,6 @@ private void addCompoundSortField(JsonParser jp, SearchContext context, List<Sor
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);
Expand Down

0 comments on commit 6fe329a

Please sign in to comment.