# Search query does not return expected records

**URL:** <https://discuss.elastic.co/t/search-query-does-not-return-expected-records/281941>\
**Category:** Elasticsearch\
**Created:** [August 19, 2021, 9:56am UTC](https://discuss.elastic.co/t/search-query-does-not-return-expected-records/281941 "2021-08-19T09:56:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rammi22](https://avatars.discourse-cdn.com/v4/letter/r/258eb7/32.png) [@Rammi22](https://discuss.elastic.co/u/Rammi22)\
**Post date:** [August 19, 2021, 9:56am UTC](https://discuss.elastic.co/t/search-query-does-not-return-expected-records/281941/1 "2021-08-19T09:56:15Z")

</div>

Hello,

i have documents with following structure:

```auto
...
// doc 1
"_source": {
		...,
		"data": {
			"main": {
                ...
				"refAccount1": 1,
				"refAccount2": 1,
				"refAccount3": 1
				"refType": 1,
			},
			"detail": {
                ...
				"feld1": "Firstname",
				"feld2": "Surname",
				"feld3": "Avatername",
				"feld4": "test",
				...
			}
		}
	}
// doc 2
   ...
   "_source": {
		...,
		"data": {
			"main": {
                ...
				"refAccount1": 2,
				"refAccount2": 3,
				"refAccount3": 3
				"refType": 1,
			},
			"detail": {
                ...
				"feld1": "Firstname",
				"feld2": "Surname",
				"feld3": "Avatername",
				"feld4": "test",
				...
			}
		}
	}
// doc 3
   ...
   "_source": {
		...,
		"data": {
			"main": {
                ...
				"refAccount1": 4,
				"refAccount2": 3,
				"refAccount3": 3
				"refType": 1,
			},
			"detail": {
                ...
				"feld1": "no",
				"feld2": "Surname",
				"feld3": "false",
				"feld4": "test",
				...
			}
		}
	}

```

All `detail`-fields defined as `wildcards`.

With the term `*name*` i try to return all records, where `detail`-wildcard match and with following conditions using `terms`-query:

`refType` **must** match the a given array value  
**AND** _at least one_ of the _Account_-Fields has to match the term o a given array value

like in in sql (fantasy code):

```auto
SELECT * FROM table
WHERE 
(
   data.detail.feld1 LIKE name
   OR 
   data.detail.feld2 LIKE name
   OR
   data.detail.feld3 LIKE name
) AND  
(
   data.main.refAccount1 IN (1,2)
   OR
   data.main.refAccount2 IN (1,2)
   OR
   data.main.refAccount3 IN (1,2)
) AND
(
   data.main.refType IN (1,2,3,4,5,6)
)

```

**Expected** : `doc1`, `doc2`

Trying with combined `should` & `filter` does not return the expected result:

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "wildcard": {
            "data.detail.feld1": {
              "value": "*name*",
              "case_insensitive": true
            }
          }
        },
        {
          "wildcard": {
            "data.detail.field2": {
              "value": "*name*",
              "case_insensitive": true
            }
          }
        },
        {
          "wildcard": {
            "data.detail.field3": {
              "value": "*name*",
              "case_insensitive": true
            }
          }
        },
        {
          "terms": {
            "data.main.refAccount1": ["1", "2"]
          }
        },
        {
          "terms": {
            "data.main.refAccount2": ["1", "2"]
          }
        },
        {
          "terms": {
            "data.main.refAccount3": ["1","2"]
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "data.main.refType": ["1", "2", "3", "4", "5"]
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "from": 0,
  "size": 10
}

```

**Result** returns unexpected `doc3`

I undertand, this happens because of `data.detail.field2` match the wildcard.

But how can i **exclude** `doc3` because of **not matching** the `refAccount*`?

Thx for any help

---

<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 16, 2021, 9:56am UTC](https://discuss.elastic.co/t/search-query-does-not-return-expected-records/281941/2 "2021-09-16T09:56:18Z")

</div>

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