4

I am building API. I have id,name,price in elasticsearch. Client provides me input json with filters to be applied.

Input 1: Below user is filtering records with id=1 (integer)

{
    "filters": {
        "id":1
    }

}

Input 2: User is querying for records with city=tokyo

{
    "filters": {
        "city":"tokyo"
    }

}

Java code for handling inputs and querying to elastic search

        filters = ipjson.path("filters");
        Iterator<Entry<String, JsonNode>> ite = filters.fields();

        while (ite.hasNext()) {
            Entry<String, JsonNode> ele = ite.next();
            String key = ele.getKey();
            if (ele.getValue().isInt()) {
                andFilter.add(FilterBuilders.termFilter(key, ele.getValue().asInt()))

            } else if (ele.getValue().isTextual()) {
                andFilter.add(FilterBuilders.termFilter(key, ele.getValue().textValue()));
            }


        }

For each key received in filters, I am checking data type of input value.

I want to support all columns. I want a generic solution without checking for input data type.

Input 3:

{
        "filters": {
            "city":"tokyo",
            "id":3,
            "price":134.45
        }

}

Without any handling for each field vs their data type, I want to query above filters in elasticsearch with java API. How would I do that?

Update:

Trying to send all string parameters to elastic search, getting below exception

SearchParseException[[project][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"filtered":{"filter":{"and":{"filters":[{"term":{"id":"\"1\""}}]}}}}}]]]; nested: NumberFormatException[For input string: ""1""]; }
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:233) ~[elasticsearch-1.4.1.jar:na]
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:179) ~[elasticsearch-1.4.1.jar:na]
    at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:565) ~[elasticsearch-1.4.1.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.7.0_60]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.7.0_60]
Kunal Pradhan
  • 522
  • 1
  • 5
  • 12
  • Why do you need to convert the values to their concrete data types? You can just leave them as strings. – Lital Kolog Jun 14 '15 at 11:04
  • If I dont convert or keep it string then it will throw exception in result. Data type mismatch. – Kunal Pradhan Jun 14 '15 at 11:37
  • Please post your exception, maybe you need to configure you index before indexing. – Lital Kolog Jun 14 '15 at 12:03
  • I have updated my question with exception I am getting on passing string in id query – Kunal Pradhan Jun 14 '15 at 16:45
  • @KunalPradman can you post the code that lead to this exception? It doesn't seem to be the same code as above. From the error it looks like you have double quotes that don't need to be there. – Lital Kolog Jun 15 '15 at 05:07
  • @Lital double quotes came from user input. I am building an API in which my clients php,android will send input. Assume client is sending double quotes in Id field. I want ES to handle it. – Kunal Pradhan Jun 15 '15 at 05:22

1 Answers1

1

When you use termFilter for FilterBuilder you dont need to specify the values explicitly as this supports all the basic primitive Java types such as (float,int,long,string,object). So just use FilterBuilders.termFilter(key, ele.getValue()) and apache lucene engine takes care of the type.

Rohith K
  • 1,433
  • 10
  • 15
  • Do I need to configure anything in elastic search for this? I tried this but throwed exception for data type – Kunal Pradhan Jun 14 '15 at 13:05
  • I have updated my question with exception I am getting on passing string in id query – Kunal Pradhan Jun 14 '15 at 16:45
  • ""1"" the extra quotes on the input makes lucene hard to convert to a data type it is known about. – Rohith K Jun 15 '15 at 06:06
  • But this input is from clients i.e. PHP, Android, Postman. They post a json to Java API, Java API builds ES query dynamically. How do I achieve it? – Kunal Pradhan Jun 15 '15 at 06:14
  • I think you can make use of the Guava plugin and remove the extra quotes for the string. eg : CharMatcher.is('\"').trimFrom("stringvalue"); – Rohith K Jun 15 '15 at 06:47