# Multi-Fields search using Span Queries with fuzziness in Elasticsearch

**URL:** <https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596>\
**Category:** Elasticsearch\
**Created:** [October 16, 2018, 7:34am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596 "2018-10-16T07:34:14Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 7:34am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/1 "2018-10-16T07:34:14Z")

</div>

Hi all,  
I am using Span queries to enable match phrase with fuzziness. I am able to do this on single field but since i am using fuzzy query with span\_multi query, I am failing it to use this query for multiple fields based search. Is there a way that i can overcome this issue?

```
{
      "from": 0,
      "size": 10,
      
     "query": {
            "span_near" : {
                "clauses" : [
                   { 
                   	"span_multi": {
                           "match": {
                                 "fuzzy": {
                                    "TITLE": {
                                           "fuzziness": "2",
                                            "value": "claims"
                                                      }
                                            }
                                      }
                               }
                   },
                     { 
                   	"span_multi": {
                           "match": {
                                 "fuzzy": {
                                    "TITLE": {
                                           "fuzziness": "2",
                                            "value": "novation"
                                                      }
                                            }
                                      }
                               }
                   }
                   
                ],
                "slop" : 2,
                "in_order" : true
            }
        },
      "highlight": {
        "type": "fvh",
        "fields": {
          "*": {}
        }
      }
    }
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 16, 2018, 8:39am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/2 "2018-10-16T08:39:06Z")

</div>

I can think of a couple of options:

- **Query-time** : use a `bool` query with a `should` array containing versions of your span query written for each field
- **Index-time:** use `copy_to` in your mapping to combine the content of multiple fields into a single searchable field and then search just that

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 8:51am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/3 "2018-10-16T08:51:13Z")

</div>

Hi, thanks for your time and response  
I have about 150 fields and thousands of documents, so i don't think Index time operation would not be feasible due to the large index size.  
can you please provide a link or an example where bool and should array is used with span queries or either the format of its usage?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 16, 2018, 8:55am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/4 "2018-10-16T08:55:29Z")

</div>

> [@Nikesh](#):
>
> can you please provide a link or an example where bool and should array

The [bool query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) provides the building blocks for assembling combinations of all queries (span included) using Boolean logic. You want an OR (span match on field X OR field Y) so you need boolean logic to assemble that expression.

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 9:00am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/5 "2018-10-16T09:00:13Z")

</div>

By what I understood, I have to use span query under bool query. Where do i mention all fields in this query? since span query doesn't support fields operator

```
  {
      "from": 0,
      "size": 10,
      
     "query": {
     	"bool": {
     		"should":{
     		  "span_near" : {
                "clauses" : [
                   { 
                   	"span_multi": {
                           "match": {
                                 "fuzzy": {
                                    "TITLE": {
                                           "fuzziness": "2",
                                            "value": "claims"
                                                      }
                                            }
                                      }
                               }
                   },
                     { 
                   	"span_multi": {
                           "match": {
                                 "fuzzy": {
                                    "TITLE": {
                                           "fuzziness": "2",
                                            "value": "novation"
                                                      }
                                            }
                                      }
                               }
                   }
                   
                ],
                "slop" : 2,
                "in_order" : true
            }
     		}
     	}
          
        },
      "highlight": {
        "type": "fvh",
        "fields": {
          "*": {}
        }
      }
    }
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 16, 2018, 9:03am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/6 "2018-10-16T09:03:37Z")

</div>

> [@Nikesh](#):
>
> Where do i mention all fields in this query?

You have to repeat yourself. Multiple span query objects, each with a different field name (to provide the relevant context) but containing the same search terms ie

> [@Mark\_Harwood](#):
>
> a `should` array containing versions of your span query written for each field

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 9:13am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/7 "2018-10-16T09:13:08Z")

</div>

> [@Mark\_Harwood](#):
>
> You have to repeat yourself. Multiple span query objects, each with a different field name (to provide the relevant context) but containing the same search terms ie

I am looking for a query similar to this using span queries. Since i have about 150 fields, I don't feel it right to have 150 span query objects. Is there a way to mention "fields" operator like in the below example?

```
 "should": [
        {
          "multi_match": {
            "query": "there",
           "fields": [
            ]
          }
        }
]

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 16, 2018, 9:14am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/8 "2018-10-16T09:14:05Z")

</div>

> [@Nikesh](#):
>
> Is there a way to mention "fields" operator like in the below example?

No, sorry. Span is used on text fields and generally an index has many structured fields but only one or two unstructured text fields to capture what can't be expressed in structured data.

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 9:18am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/9 "2018-10-16T09:18:12Z")

</div>

Thanks for your honest response.  
Is there a another method where i can use fuzziness with match\_phrase query?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 16, 2018, 9:27am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/10 "2018-10-16T09:27:35Z")

</div>

You perhaps don't need a phrase/span query that supports fuzzy.  
A common strategy is to have a big `bool` query with a `should` array filled with different forms of running the same user input, ranging from the sloppy "any word plus fuzzy" to the very strict e.g. ANDed terms or exact phrase matches. Docs which satisfy more of the given clauses will naturally rank higher.

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 10:30am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/11 "2018-10-16T10:30:22Z")

</div>

I did follow the same strategy. But it is taking a lot of time to process for a single document.

```
{
  "from": 0,
  "size": 24,
  "query": {
    "bool": {
      
      "should": [
        {
          "multi_match": {
            "query": "current",
            "type": "best_fields",
            "fields": []
          }
        },
        {
          "query_string": {
            "query": "*current*",
            "fields": []
          }
        },
        {
          "multi_match": {
            "query": "current",
            "fuzziness": "1",
            "fields": []
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "highlight": {
    "type": "fvh",
    "fields": {
      "*": {}
    }
  }
}

```

I did post an query about this : [Highlighting with fvh taking long time in Elasticsearch](https://discuss.elastic.co/t/highlighting-with-fvh-taking-long-time-in-elasticsearch/152448)

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 16, 2018, 10:34am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/12 "2018-10-16T10:34:58Z")

</div>

Try taking away request elements until you find the culprit. Wildcards and highlighting are both potential performance hogs. You could also try the `unified` highlighter type. "Fast Vector Highlighter" isn't always as fast as the name suggests.

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 10:59am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/13 "2018-10-16T10:59:59Z")

</div>

My project demands fuzziness, wildcard, highlighting. Indeed unified is faster than fvh. Will unified highlighting be deprecated in near future? Unified takes around 4571ms for a single document. Is it because of the edge gram analyzer that i have used?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 16, 2018, 11:06am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/14 "2018-10-16T11:06:06Z")

</div>

This is straying from the original topic. In the interests of keeping things focused I suggest opening another issue to concentrate on questions around highlighter performance. It would help to do some investigation with your data and settings first eg the effects of ngram sizes/numbers of fields otherwise you'll just be waiting for someone to ask your for that additional information before being able to offer a diagnosis.

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [October 16, 2018, 11:23am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/15 "2018-10-16T11:23:25Z")

</div>

Thank you

> [@Highlighting with fvh and fuzziness taking long time in Elasticsearch](https://discuss.elastic.co/t/highlighting-with-fvh-taking-long-time-in-elasticsearch/152448):
>
> Hi all, I have indexed a single document with more than 150 metadata, each with mapping: "ACTIVE": { "type": "text", "term\_vector": "with\_positions\_offsets", "fields": { "autocomplete\_analyzed": { "type": "text", "analyzer": "autocomplete" …

---

<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:** [November 13, 2018, 11:24am UTC](https://discuss.elastic.co/t/multi-fields-search-using-span-queries-with-fuzziness-in-elasticsearch/152596/16 "2018-11-13T11:24:28Z")

</div>

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