# Search for documents with exactly different fields values

**URL:** <https://discuss.elastic.co/t/search-for-documents-with-exactly-different-fields-values/144586>\
**Category:** Elasticsearch\
**Created:** [August 15, 2018, 9:17pm UTC](https://discuss.elastic.co/t/search-for-documents-with-exactly-different-fields-values/144586 "2018-08-15T21:17:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ranieri\_Mazili\_de\_Ol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ranieri_mazili_de_ol/32/15815_2.png) [@Ranieri\_Mazili\_de\_Ol](https://discuss.elastic.co/u/Ranieri_Mazili_de_Ol)\
**Post date:** [August 15, 2018, 9:17pm UTC](https://discuss.elastic.co/t/search-for-documents-with-exactly-different-fields-values/144586/1 "2018-08-15T21:17:10Z")

</div>

I'm adding documents with the following strutucte

```
{
    "proposta": {
        "matriculaIndicacao": 654321,
        "filial": 100,
        "cpf": "12345678901",
        "idStatus": "3",
        "status": "Reprovada",
        "dadosPessoais": {
            "nome": "John Five",
            "dataNascimento": "1980-12-01",
            "email": "fulanodasilva@fulano.com.br",
            "emailValidado": true,
            "telefoneCelular": "11 99876-9999",
            "telefoneCelularValidado": true,
            "telefoneResidencial": "11 2211-1122",
            "idGenero": "1",
            "genero": "M"
        }
    }
}

```

I'm trying to perform a search with multiple field values.  
I can successfull search for a document with a specific cpf atribute with the following search  
{  
"query": {  
"term" : {  
"proposta.cpf" : "23798770823"  
}  
}  
}

But now I need to add an AND clause, like

```
{
  "query": {
    "term" : { 
    	"proposta.cpf" : "23798770823"
    	,"proposta.dadosPessoais.dataNascimento": "1980-12-01"
    }
  }
}

```

but it's returning an error message.  
P.S: If possible I would like to perform a search where if the field doesn't exist, it returns the document that matches only the proposta.cpf field.

I really appreciate any help.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 17, 2018, 8:50am UTC](https://discuss.elastic.co/t/search-for-documents-with-exactly-different-fields-values/144586/2 "2018-08-17T08:50:26Z")

</div>

Take a look at the [bool query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html). That is the query that you can use to combine multiple search criteria.

To implement your AND query, you would use the `must` clause of the bool query. Put the queries that must match for a document to be considered a hit in this must clause:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "proposta.cpf": "23798770823"
          }
        },
        {
          "term": {
            "proposta.dadosPessoais.dataNascimento": "1980-12-01"
          }
        }
      ]
    }
  }
}

```

Your second requirement, where a field the `proposta.dadosPessoais.dataNascimento` field can be either missing or it has to match a specific value, can be implemented by combining a `must` clause with a `should` clause. The `should` clause contains the "nice to have" criteria. You can put the query for `proposta.dadosPessoais.dataNascimento` in there, along with a bool query that uses a `must_not` to search for documents that are missing the value using the [`exists` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html#_literal_missing_literal_query). Next, you tell Elasticsearch that either one of these criteria has to match using `"minimum_should_match": 1`:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "proposta.cpf": "23798770823"
          }
        }
      ],
      "should": [
        {
          "term": {
            "proposta.dadosPessoais.dataNascimento": "1980-12-01"
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "proposta.dadosPessoais.dataNascimento"
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

```

This all may look a bit complicated at first, as the boolean logic of the bool query can take some getting used to. But if you read the docs and play around with it, you'll soon understand its power.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 14, 2018, 8:50am UTC](https://discuss.elastic.co/t/search-for-documents-with-exactly-different-fields-values/144586/3 "2018-09-14T08:50:27Z")

</div>

Closed on September 14, 2018, 8:50am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
