2021-03-31

Should and Filter combination in ElasticSearch

I have this query which return the correct result

GET /person/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "fuzzy": {
            "nameDetails.name.nameValue.surname": {
              "value": "Pibba",
              "fuzziness": "AUTO"
            }
          }
        },
        {
          "fuzzy": {
            "nameDetails.nameValue.firstName": {
              "value": "Fawsu",
              "fuzziness": "AUTO"
            }
          }
        }
      ]
    }
  }
}

and the result is below:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.6012557,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "70002",
        "_score" : 3.6012557,
        "_source" : {
          "gender" : "Male",
          "activeStatus" : "Inactive",
          "deceased" : "No",
          "nameDetails" : {
            "name" : [
              {
                "nameValue" : {
                  "firstName" : "Fawsu",
                  "middleName" : "L.",
                  "surname" : "Pibba"
                },
                "nameType" : "Primary Name"
              },
              {
                "nameValue" : {
                  "firstName" : "Fausu",
                  "middleName" : "L.",
                  "surname" : "Pibba"
                },
                "nameType" : "Spelling Variation"
              }
            ]
          }              
        }
      }
    ]
  }

But when I add the filter for Gender, it returns no result

GET /person/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "fuzzy": {
            "nameDetails.name.nameValue.surname": {
              "value": "Pibba",
              "fuzziness": "AUTO"
            }
          }
        },
        {
          "fuzzy": {
            "nameDetails.nameValue.firstName": {
              "value": "Fawsu",
              "fuzziness": "AUTO"
            }
          }
        }
      ],
      "filter": [
        {
          "term": {
            "gender": "Male"
          }
        }
      ]
    }
  }
}

Even I just use filter, it return no result

GET /person/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "gender": "Male"
          }
        }
      ]
    }
  }
}


from Recent Questions - Stack Overflow https://ift.tt/2OarJRO
https://ift.tt/eA8V8J

No comments:

Post a Comment