Query to ignore fields with empty content

Ok, lets try this.
Btw I added a 4th document, that has "audit" in trigger_terms but not in query_terms. This is another negative case, just to ensure we cover all scenarios.
We need something like ... (query_terms="audit" AND (trigger_terms="audit OR trigger_terms="")
So here is the query that worked for me ...

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "query_terms": "audit"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must_not": [
                                        {
                                            "exists": {
                                                "field": "trigger_terms"
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "trigger_terms": "audit"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

I see the following correct results ...

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.287682,
    "hits": [
      {
        "_index": "test1",
        "_type": "concepts",
        "_id": "3",
        "_score": 1.287682,
        "_source": {
          "query_terms": [
            "best",
            "fit",
            "perfect",
            "audit"
          ],
          "trigger_terms": [],
          "suggestions": [
            "double jeopardy",
            "same elements",
            "same evidence"
          ]
        }
      },
      {
        "_index": "test1",
        "_type": "concepts",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "query_terms": [
            "audit",
            "audits",
            "audited"
          ],
          "trigger_terms": [
            "audit",
            "return"
          ],
          "suggestions": [
            "examination",
            "inspection",
            "investigation"
          ]
        }
      }
    ]
  }
}
1 Like