Span multi-term query

Span multi-term query

The span_multi query allows you to wrap a multi term query (one of wildcard, fuzzy, prefix, range or regexp query) as a span query, so it can be nested. Example:

  1. resp = client.search(
  2. query={
  3. "span_multi": {
  4. "match": {
  5. "prefix": {
  6. "user.id": {
  7. "value": "ki"
  8. }
  9. }
  10. }
  11. }
  12. },
  13. )
  14. print(resp)
  1. response = client.search(
  2. body: {
  3. query: {
  4. span_multi: {
  5. match: {
  6. prefix: {
  7. 'user.id' => {
  8. value: 'ki'
  9. }
  10. }
  11. }
  12. }
  13. }
  14. }
  15. )
  16. puts response
  1. const response = await client.search({
  2. query: {
  3. span_multi: {
  4. match: {
  5. prefix: {
  6. "user.id": {
  7. value: "ki",
  8. },
  9. },
  10. },
  11. },
  12. },
  13. });
  14. console.log(response);
  1. GET /_search
  2. {
  3. "query": {
  4. "span_multi": {
  5. "match": {
  6. "prefix": { "user.id": { "value": "ki" } }
  7. }
  8. }
  9. }
  10. }

A boost can also be associated with the query:

  1. resp = client.search(
  2. query={
  3. "span_multi": {
  4. "match": {
  5. "prefix": {
  6. "user.id": {
  7. "value": "ki",
  8. "boost": 1.08
  9. }
  10. }
  11. }
  12. }
  13. },
  14. )
  15. print(resp)
  1. response = client.search(
  2. body: {
  3. query: {
  4. span_multi: {
  5. match: {
  6. prefix: {
  7. 'user.id' => {
  8. value: 'ki',
  9. boost: 1.08
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. )
  17. puts response
  1. const response = await client.search({
  2. query: {
  3. span_multi: {
  4. match: {
  5. prefix: {
  6. "user.id": {
  7. value: "ki",
  8. boost: 1.08,
  9. },
  10. },
  11. },
  12. },
  13. },
  14. });
  15. console.log(response);
  1. GET /_search
  2. {
  3. "query": {
  4. "span_multi": {
  5. "match": {
  6. "prefix": { "user.id": { "value": "ki", "boost": 1.08 } }
  7. }
  8. }
  9. }
  10. }

span_multi queries will hit too many clauses failure if the number of terms that match the query exceeds the indices.query.bool.max_clause_count search setting. To avoid an unbounded expansion you can set the rewrite method of the multi term query to top_terms_* rewrite. Or, if you use span_multi on prefix query only, you can activate the index_prefixes field option of the text field instead. This will rewrite any prefix query on the field to a single term query that matches the indexed prefix.