# Wildcard query with second condition on other field

**URL:** <https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671>\
**Category:** Elasticsearch\
**Created:** [February 27, 2018, 2:03pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671 "2018-02-27T14:03:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michele](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@michele](https://discuss.elastic.co/u/michele)\
**Post date:** [February 27, 2018, 2:03pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671/1 "2018-02-27T14:03:37Z")

</div>

I would execute a query like this (sql like):

`SELECT * FROM MYINDEX WHERE SUGGEST LIKE %FOO% AND YEAR=1999`

How can I do in Elastic Search?

I write a query with wild card but I don't know how to combine it with and clause that match year=1999:

```
{
    "query": {
        "wildcard" : { "name" : "*foo*" }
    }
}

```

Can you help me?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 27, 2018, 2:05pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671/2 "2018-02-27T14:05:26Z")

</div>

Use a bool query.

(And don't use wildcards but this is another story)

---

<div class="post-metadata">

**Author:** ![michele](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@michele](https://discuss.elastic.co/u/michele)\
**Post date:** [February 27, 2018, 2:21pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671/3 "2018-02-27T14:21:00Z")

</div>

This is my index:  
{  
"mappings": {  
"records" : {  
"properties" : {  
"suggest" : {  
"type" : "completion",  
"contexts": [  
{  
"name": "year",  
"type": "category",  
"path": "year"  
}  
]  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 27, 2018, 2:36pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671/4 "2018-02-27T14:36:50Z")

</div>

> How can be done with this mappings?

What can be done? The query part?

---

<div class="post-metadata">

**Author:** ![michele](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@michele](https://discuss.elastic.co/u/michele)\
**Post date:** [February 27, 2018, 2:58pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671/5 "2018-02-27T14:58:40Z")

</div>

Yes, I would do the query with `LIKE %FOO% AND YEAR=1999`

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 27, 2018, 6:39pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671/6 "2018-02-27T18:39:44Z")

</div>

So:

Something like:

```auto
POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "wildcard" : { "name" : "%FOO%" }
      },
      "filter": {
        "term" : { "year" : 1999 }
      }
    }
  }
}

```

Some note about wildcard from doc: [Wildcard Query | Elasticsearch Reference [6.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-wildcard-query.html)

> Note that this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow wildcard queries, a wildcard term should not start with one of the wildcards \* or ?.

---

<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:** [March 27, 2018, 6:39pm UTC](https://discuss.elastic.co/t/wildcard-query-with-second-condition-on-other-field/121671/7 "2018-03-27T18:39:57Z")

</div>

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