# Query performance with span queries

**URL:** <https://discuss.elastic.co/t/query-performance-with-span-queries/360411>\
**Category:** Elastic Search\
**Created:** [May 28, 2024, 8:12pm UTC](https://discuss.elastic.co/t/query-performance-with-span-queries/360411 "2024-05-28T20:12:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattstack](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@mattstack](https://discuss.elastic.co/u/mattstack)\
**Post date:** [May 28, 2024, 8:12pm UTC](https://discuss.elastic.co/t/query-performance-with-span-queries/360411/1 "2024-05-28T20:12:29Z")

</div>

0

I'm currently working on improving the accuracy of results returned from an Elasticsearch query by incorporating a "body" field into the search along with search fuzzyness. The website in question has a lot of content and is highly trafficked. I rebuilt the query using span\_multi and span\_near queries for improved accuracy with fuzzysearch, but it seems the rewrite overloaded my Elasticsearch servers and stopped search from working. Any pointers or observations on the existing and updated queries below?

The Elasticsearch server version is: 7.5.2 and admittedly outdated.

Existing Query:

```auto
return {
  query: {
    bool: {
      must: [
        {
          dis_max: {
            queries: [
              { match: { title_search: { query: keyword, boost: 60 } } },
              {
                wildcard: {
                  title_search: { value: '*' + keyword + '*', boost: 40 },
                },
              },
            ],
            tie_breaker: 0.7,
            boost: 5,
          },
        },
      ],
      filter: [
        fieldDateFilter,
        { term: { status: true } },
        { term: { type: contentType } },
        { term: { langcode: language } },
      ],
    },
  },
  size: 1000,
  sort: [{ _score: { order: 'desc' } }],
}

```

Updated Query:

```auto
function getContentTypeSearchAllResult(
  keyword,
  language,
  contentType,
  dateFilter
) {
  keyword = String(decodeURI(keyword)).toLowerCase().replace(/\+/g, ' ');

  // Setup span queries.
  const keywordWords = keyword.split(' ');
  const title_search_queries = [];
  const body_search_queries = [];

  // Construct title_search span queries for each word in the keyword.
  keywordWords.forEach(word => {
    const wordQuery = {
      span_multi: {
        match: {
          fuzzy: {
            title_search: {
              value: word,
              fuzziness: 1,
            },
          },
        },
      },
    };

    title_search_queries.push(wordQuery);
  });

  // Construct body span queries for each word in the keyword.
  keywordWords.forEach(word => {
    const wordQuery = {
      span_multi: {
        match: {
          fuzzy: {
            body: {
              value: word,
              fuzziness: 1,
            },
          },
        },
      },
    };

    body_search_queries.push(wordQuery);
  });

  const fieldDateFilter = contentType.includes('event')
    ? {
        range: {
          field_date: {
            lt:
              dateFilter !== 'all' && dateFilter !== null
                ? ElasticSearch[dateFilter]
                : undefined,
            gte:
              dateFilter !== 'all' && dateFilter !== null
                ? dateFilter.includes('month')
                  ? 'now/M'
                  : 'now'
                : undefined,
          },
        },
      }
    : undefined;

  return {
    query: {
      bool: {
        must: [
          {
            dis_max: {
              queries: [
                {
                  span_near: {
                    clauses: title_search_queries,
                    slop: 0,
                    in_order: false,
                    boost: 60,
                  },
                },
                {
                  wildcard: {
                    title_search: { value: '*' + keyword + '*', boost: 40 },
                  },
                },
                {
                  span_near: {
                    clauses: body_search_queries,
                    slop: 0,
                    in_order: false,
                    boost: 5,
                  },
                },
              ],
              tie_breaker: 0.7,
              boost: 5,
            },
          },
        ],
        filter: [
          fieldDateFilter,
          {
            term: {
              status: true,
            },
          },
          { term: { type: contentType } },
          { term: { langcode: language } },
        ],
      },
    },
    size: 1000,
    sort: [{ _score: { order: 'desc' } }],
  };
}

```

Expectation: The Elasticsearch server would not stop working.

---

<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:** [June 25, 2024, 8:13pm UTC](https://discuss.elastic.co/t/query-performance-with-span-queries/360411/2 "2024-06-25T20:13:19Z")

</div>

Closed on June 25, 2024, 8:13pm UTC

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