2

I need to query some data from aws elasticsearch with some condition.

I use the BoolQueryBuilder

And the params is about pretty=true

here is my code:

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
QueryBuilder qb1 = QueryBuilders.matchQuery("personId", personId);
boolQueryBuilder.must(qb1);
sourceBuilder.query(boolQueryBuilder);
result = searchUtil.getResult(params, sourceBuilder);

the following is the error message

{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "Unknown key for a START_OBJECT in [queryBuilder].",
        "line" : 1,
        "col" : 17
      }
    ],
    "type" : "parsing_exception",
    "reason" : "Unknown key for a START_OBJECT in [queryBuilder].",
    "line" : 1,
    "col" : 17
  },
  "status" : 400
}

can anyone help?! please!

1
  • searchUtil.getResult is your own method ? Commented Dec 17, 2019 at 10:41

1 Answer 1

2

Not sure what is the use of result = searchUtil.getResult(params, sourceBuilder); in your code.

You can read this official ES doc for constructing the queries using the query builders which you are using also refer this official ES example to construct your query.

It's very simple and my guess the reason for your error is(As complete information isn't available) that you are using some other utility to get the result which is unable to process your JSON.

Edit: Added code that worked locally for me using java high-level rest-client version 7.4 of Elasticsearch.

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    QueryBuilder qb1 = QueryBuilders.matchQuery("personId", "kimchy");
    boolQueryBuilder.must(qb1);
    sourceBuilder.query(boolQueryBuilder);
    //String searchJson = sourceBuilder.toString(); // if you want to print search json for debugging
    SearchRequest searchRequest = new SearchRequest(indexName);// your index name
    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

Please note last three lines of code are very important which is missing in your code and these are used to create a search request using query and actually firing this search request using the ES client and getting the result.

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

4 Comments

Hi! i try your code and it works! By the way, the response is all bytes, how can i transform to text?!
i saw the document and solved the problem thank you so much !!
I ’m not really sure how to use it. is that accept and vote now?
It is deprecated now

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.