Home > Mobile >  Elasticsearch query not returning results
Elasticsearch query not returning results

Time:01-18

I am new to elasticsearch and I am using an existing infrastructure, I am trying to understand what is wrong with my query because I receive no results.

I have the following model -

X = {
    "id" : "string",
    "name" : "string",
    "hidden" : boolean,
    "description" : "string",
    "status" : "string",
    "type" : "string"
}

I am trying to create a condition that will exclude results - under "must_not" I have created a bool query that has a "must" that contains 3 terms.

Pseudo code of what I am trying to achieve -

IF X.id IN [] AND x.hidden == true AND x.status == "FINISHED" THEN don't score -- only if the entire condition is met.

Even though I have data that should be returning, I am receiving nothing. Any assist will be great. Thanks.

Query -

{
  "bool" : {
    "must" : [
      {
        "multi_match" : {
          "query" : "{phrase}",
          "fields" : [
            "searchData.body*^1.0",
            "searchData.description*^3.0",
            "searchData.name*^4.0",
            "searchData.taskBody*^1.0",
            "searchData.topics^2.0"
          ],
          "type" : "best_fields",
          "operator" : "OR",
          "slop" : 0,
          "prefix_length" : 0,
          "max_expansions" : 50,
          "zero_terms_query" : "NONE",
          "auto_generate_synonyms_phrase_query" : true,
          "fuzzy_transpositions" : true,
          "boost" : 1.0
        }
      }
    ],
    "filter" : [
      {
        "terms" : {
          "context.X.id" : [
            "04cb64b5-bf28-4e04-a7f1-6f93a8caa10c",
            "368bf798-5ec7-4f73-904e-ec22818c4f6b",
            "8ccb61ab-364a-4303-b21f-e930bb8b8071",
            "7675e70a-1216-49d5-915c-a3c3f919a1bf",
            "c9d976d8-bbe3-44db-9d97-5fafc4af0d3f",
            "234f9f61-69fe-41e8-89e8-a5c2c4e99e0a",
            "c0a813e6-4e32-4993-a53f-bb16b16ef976",
            "261da1c2-edc5-4f51-807e-603fe9d9712a",
            "dc2d1e94-6653-420c-96ce-bccdd7e187f1",
            "zzzzzzzz-xxxx-4e04-a7f1-yyyyyyyyyyyy",
            "xxxxxxxx-bf28-yyyy-a7f1-kkkkkkkkkkkk",
            "yyyyyyyy-bf28-4e04-a7f1-mmmmmmmmmmmm",
          ],
          "boost" : 1.0
        }
      }
    ],
    "must_not" : [
      {
        "bool" : {
          "must" : [
            {
              "term" : {
                "context.X.id" : {
                  "value" : [
                    "04cb64b5-bf28-4e04-a7f1-6f93a8caa10c",
                    "368bf798-5ec7-4f73-904e-ec22818c4f6b",
                    "8ccb61ab-364a-4303-b21f-e930bb8b8071",
                    "7675e70a-1216-49d5-915c-a3c3f919a1bf",
                    "c9d976d8-bbe3-44db-9d97-5fafc4af0d3f",
                    "234f9f61-69fe-41e8-89e8-a5c2c4e99e0a",
                    "c0a813e6-4e32-4993-a53f-bb16b16ef976",
                    "261da1c2-edc5-4f51-807e-603fe9d9712a",
                    "dc2d1e94-6653-420c-96ce-bccdd7e187f1"
                  ],
                  "boost" : 1.0
                }
              }
            },
            {
              "term" : {
                "context.X.status" : {
                  "value" : "FINISHED",
                  "boost" : 1.0
                }
              }
            },
            {
              "term" : {
                "context.X.hidden" : {
                  "value" : true,
                  "boost" : 1.0
                }
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

Edit:

Mapping:

"X": {
                "properties": {
                  "id": {
                    "type": "keyword",
                    "normalizer": "id_normalizer"
                  },
                  "description": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
----------------------------------before edit
                  "hidden": {
                    "type": "boolean"
                  },
----------------------------------
----------------------------------after edit
                  "hidden": {
                    "type": "boolean",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
----------------------------------
                  "name": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "status": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "type": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              }

CodePudding user response:

Thanks to @ilvar who got my attention to the keyword definition, I have noticed that the status field is defined as text.

This field is getting it's values from a Status enum and the string representation of that enum is being saved.

For some reason results hasn't returned until I searched for the X.status.keyword path ( ES documents mention that a field who's type keyword is not explicit so it can be several types)

  •  Tags:  
  • Related