1

I am trying to generate a query using the Java RestHighLevelClient of Elasticsearch similar to this:

GET /field_search/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "ENTRY_ID": "ttttt"
          }
        },
        {
          "match": {
            "MODULE_ID": "xxxxx"
          }
        },
        {
          "match": {
            "COMPANY_ID": "22244"
          }
        },
        {
          "match": {
            "DELETED": false
          }
        }
      ]
    }
  }
}

This is my code that I am using to generate it

    BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();

    boolQueryBuilder1.must().add(QueryBuilders.matchQuery("MODULE_ID", moduleId));

    boolQueryBuilder1.must().add(QueryBuilders.matchQuery("COMPANY_ID", companyId));

    ........

I have skipped some part of it to keep it small. But i use a BoolQueryBuilder and the query it generates is something like this:

{
  "query": {
  "bool" : {
    "must" : [
      {
        "match" : {
          "MODULE_ID" : {
            "query" : "xxxxx",
            "operator" : "OR",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : false,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : false,
            "boost" : 1.0
          }
        }
      },
      {
        "match" : {
          "COMPANY_ID" : {
            "query" : "22244",
            "operator" : "OR",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : false,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : false,
            "boost" : 1.0
          }
        }
      },
      {
        "match" : {
          "DELETED" : {
            "query" : false,
            "operator" : "OR",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : false,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : false,
            "boost" : 1.0
          }
        }
      },
      {
        "match" : {
          "ENTRY_ID" : {
            "query" : ttttt,
            "operator" : "OR",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : false,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : false,
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
}

It adds additional things into the query. Using the above normal query my results come back correctly but with the java generated query my results are none so how can i build the same query using Java client?

Shashank
  • 247
  • 5
  • 17

1 Answers1

0
"adjust_pure_negative" : true 

This is your problem, set it to false or delete it.

Read here why this happens.

Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43