Eduardo Henrique
Eduardo Henrique

Reputation: 1

Elasticsearch: Minimum_should_match don´t return correctly

I'm new to the elastic universe and I have a question about a query. I'll try to describe it here:

I have a document called 'store' with several stores registered and within each store item a list of customers:

loja {
  nome,
  telefone,
  email,
  clientes : [
   {
      nomeCliente,
      telefone,
      email
   }
  ]
}

I need a query where I would have to return at least 1 pair of customers from the same registered store

For example:

I research 'Ana Maria', 'Sandra Maria' and 'Alberto Braz', where I would need to return the stores that have [Ana Maria and Sandra Maria] or [Ana Maria and Alberto Braz] or [Sandra Maria and Alberto Braz].

I did the search according to the dsl below, but the minimum_should_match clause is not respecting the limit of 2 m and returning results with only 1 record found. Am I doing something wrong in the query?

Could you help me out on this one?

Query:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool":{
                "should": {
                    "match": {
                        "clientes.nomeCliente" : {
                          "query" : "ANA MARIA",
                          "type" : "phrase",
                          "operator": "and",
                          "slop" : 40
                        }
                    }
                },
                "should": {
                    "match":{
                        "clientes.nomeCliente" : {
                          "query" : "SANDRA MARIA",
                          "type" : "phrase",
                          "operator": "and",
                          "slop" : 40
                        }
                    }
                },
                "should": {
                    "match":{
                        "clientes.nomeCliente" : {
                          "query" : "ALBERTO BRAZ",
                          "type" : "phrase",
                          "operator": "and",
                          "slop" : 40
                        }
                    }
                }
              },"minimum_should_match": 2
            },
            "path": "clientes",
            "inner_hits" : {
                "size" : 10
            }
          }
        }
      ]
    }
  }
}

Upvotes: 0

Views: 366

Answers (1)

hkulekci
hkulekci

Reputation: 1942

For the should you need to use an array instead of an object. So, you query need to be something like this :

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "clientes.nomeCliente": {
                        "query": "ANA MARIA",
                        "type": "phrase",
                        "operator": "and",
                        "slop": 40
                      }
                    }
                  },
                  {
                    "match": {
                      "clientes.nomeCliente": {
                        "query": "SANDRA MARIA",
                        "type": "phrase",
                        "operator": "and",
                        "slop": 40
                      }
                    }
                  },
                  {
                    "match": {
                      "clientes.nomeCliente": {
                        "query": "ALBERTO BRAZ",
                        "type": "phrase",
                        "operator": "and",
                        "slop": 40
                      }
                    }
                  }
                ],
                "minimum_should_match": 2
              }
            },
            "path": "clientes",
            "inner_hits": {
              "size": 10
            }
          }
        }
      ]
    }
  }
}

I could not check the parameters of the match query because I don't have the mapping and sample data. But you can check that part with your index directly.

Upvotes: 2

Related Questions