0

I have a problem with understanding how to make json object in java. I have to get certain data from elasticsearch through java and I also have a json that functions, but I have no idea how to make such json with functions available.

Is it possible to do this with the SearchResponse class and its functions? Might QueryBuilder help?

This works with the Sense plugin in chrome:

GET _search 
{
 "query": {
     "constant_score": {
        "filter": {
            "and": {
               "filters": [
                  {"term": 
                    {"_type":"budget"}                      
                  },
                  {"term": 
                    {"_index":"dbs_project"}                      
                  },
                  {"term": 
                    {"month":"x"}                      
                  },
                  {"term": 
                    {"account_id":"y"}                      
                  }
               ]
            }
        }            
     }
  }
}

2 Answers 2

1

QueryBuilder should definitely work for you. To build a query like you require using the API, you will need to do something like this:

QueryBuilder query = QueryBuilders.constantScoreQuery(FilterBuilders.andFilter(FilterBuilders.termFilter("_type", "budget"),
                FilterBuilders.termFilter("_index", "dbs_project"), FilterBuilders.termFilter("month", "x"),
                FilterBuilders.termFilter("account_id", "y")));

And you will use SearchResponse to execute the query and fetch results like this:

SearchResponse response = client.prepareSearch(index).setTypes(type)
                .setQuery(query).execute()
                .actionGet();

where client is the instance of your TransportClient and index and type correspond to the elasticsearch index and type you want to query from.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried XContentBuilder.

https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.6/generate.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.