1

I am having trouble making this URL query work in Java, it does not return any results. but from the browser it returns all the results, here is the URL that returns result:

_search?pretty&q=*357*+AND+account_id:574fe92c9179a809fd76f0b8+AND+invalid:false

And here is my code (does not return any results):

    FilterBuilder[] filtersArray = new FilterBuilder[2];
    filtersArray[0] = FilterBuilders.termFilter("account_id", "574fe92c9179a809fd76f0b8");
    filtersArray[1] = FilterBuilders.termFilter("invalid", false);
    QueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.simpleQueryStringQuery("*357*"), FilterBuilders.andFilter(filtersArray));
    SearchResponse response = esClient.prepareSearch(SecurityManager.getNamespace())
            .addSort("created_time", SortOrder.DESC)
            .setTypes(dataType)
            .setQuery(query)
            .addFields("_id")
            .setFrom(page * size)
            .setSize(size)
            .setExplain(false)
            .execute()
            .actionGet();

Can someone tell me what is the best way to translate the URL query into a java query?

3
  • In your URL query &account_id=574fe92c9179a809fd76f0b8&invalid=false will have no effect at all, I think you want this instead: q=*357* +AND+account_id:574fe92c9179a809fd76f0b8+AND+invalid:false. Check your results and you'll see that you have documents with a different account_id and invalid values. Commented Jun 3, 2016 at 14:18
  • OK sorry for the mistake but even after I put it in the ay mentioned I still get a total of 2256 hits, but nothing in JAVA driver :/ Commented Jun 3, 2016 at 14:23
  • I updated the question Commented Jun 3, 2016 at 14:24

1 Answer 1

2

First off, the URL query you should use is this one

?q=*357*+AND+account_id:574fe92c9179a809fd76f0b8+AND+invalid:false

otherwise you'll have no constraint on account_id and invalid

Then, the exact translation of this new URL query in Java is

QueryBuilder query = QueryBuilders.queryStringQuery("*357* AND account_id:574fe92c9179a809fd76f0b8 AND invalid:false");
SearchResponse response = esClient.prepareSearch(SecurityManager.getNamespace())
        .addSort("created_time", SortOrder.DESC)
        .setTypes(dataType)
        .setQuery(query)
        .addFields("_id")
        .setFrom(page * size)
        .setSize(size)
        .setExplain(false)
        .execute()
        .actionGet();

Notes:

  • queryStringQuery and not simpleQueryStringQuery
  • no filters as they are all in the query string already
Sign up to request clarification or add additional context in comments.

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.