# Poximity search with wildcards and phrase (multiple word terms)

**URL:** <https://discuss.elastic.co/t/poximity-search-with-wildcards-and-phrase-multiple-word-terms/228360>\
**Category:** Elasticsearch\
**Created:** [April 16, 2020, 3:50pm UTC](https://discuss.elastic.co/t/poximity-search-with-wildcards-and-phrase-multiple-word-terms/228360 "2020-04-16T15:50:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![th.wyns](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@th.wyns](https://discuss.elastic.co/u/th.wyns)\
**Post date:** [April 16, 2020, 3:50pm UTC](https://discuss.elastic.co/t/poximity-search-with-wildcards-and-phrase-multiple-word-terms/228360/1 "2020-04-16T15:50:42Z")

</div>

Hello,

We have a business requirement where the customer would like to run proximity searches that contain wildcards and phrases (multiple words).

For eg:  
query: "somet\*" within 5 word distance of "need to find"

should match: "something that we need to find"  
should not match: "something to find is the need"

How can we achieve this in Elasticsearch?

I was able to solve the wildcard part but so far I couldn't figure out how to execute it with multi word phrases.

This is what I came up so far (missing the phrase requirement):

```auto
GET basic_tests/_search
{
    "query": {
        "span_near" : {
            "clauses" : [
                { "span_multi": {"match" : {
                   "wildcard" : { "text" : { "value" : "somet*"} }
                }}},
                { "span_multi": {"match" : {
                   "prefix" : { "text" : { "value" : "this is not working for phrase"} }
                }}}
            ],
            "slop" : 5,
            "in_order" : true
        }
    }
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![th.wyns](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@th.wyns](https://discuss.elastic.co/u/th.wyns)\
**Post date:** [May 3, 2020, 6:04pm UTC](https://discuss.elastic.co/t/poximity-search-with-wildcards-and-phrase-multiple-word-terms/228360/2 "2020-05-03T18:04:44Z")

</div>

Hello,

In the meantime I was able to solve the issue. I found the following document for spans on Lucene's website:  
[https://lucene.apache.org/core/6\_2\_1/core/org/apache/lucene/search/spans/package-summary.html](https://lucene.apache.org/core/6_2_1/core/org/apache/lucene/search/spans/package-summary.html)

It has a great example at the bottom. To imitate phrase search we need to nest a "SpanNear" query with slope set to "0" and in order set to "true" and add the terms of the phrase.

This is the solution for the example I gave:

```auto
GET basic_tests/_search
{
    "query": {
        "span_near":{
            "clauses": [
                {
                  "span_multi": {
                    "match" : {
                        "wildcard" : { "text" : { "value" : "somet*"} }
                    }
                  }
                },
                {
                  "span_near" : {
                    "clauses" : [
                      { "span_term" : { "text" : "need" } },
                      { "span_term" : { "text" : "to" } },
                      { "span_term" : { "text" : "find" } }
                    ],
                    "slop" : 0,
                    "in_order" : true
                  }
                }
            ],
            "slop": 5,
            "in_order": false
        }
    }
}

```

---

<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:** [May 31, 2020, 6:04pm UTC](https://discuss.elastic.co/t/poximity-search-with-wildcards-and-phrase-multiple-word-terms/228360/3 "2020-05-31T18:04:45Z")

</div>

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