ElasticGraph Query API: Substring Filtering

Try these example queries by visiting the GraphiQL UI after booting locally.

ElasticGraph offers a predicate to support substring filtering:

contains
Matches documents using substring filtering.

When null is passed, matches all documents.

The contains operator accepts an object with multiple options. Here’s an example using anySubstringOf and ignoreCase:

query AverageSongLengthForYouOrEye {
  artistAggregations {
    nodes {
      subAggregations {
        albums {
          nodes {
            subAggregations {
              tracks(filter: {
                name: {
                  contains: {
                    anySubstringOf: ["you", "eye"]
                    ignoreCase: true
                  }
                }
              }) {
                nodes {
                  groupedBy {
                    name
                  }

                  aggregatedValues {
                    lengthInSeconds {
                      approximateAvg
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This matches tracks with names that case-insensitively contain “you” or “eye” such as:

  • Brown Eyed Girl
  • In Your Eyes
  • We Will Rock You
  • With or Without You

ElasticGraph also offers allSubstringsOf:

query AverageSongLengthForYouAndEye {
  artistAggregations {
    nodes {
      subAggregations {
        albums {
          nodes {
            subAggregations {
              tracks(filter: {
                name: {
                  contains: {
                    allSubstringsOf: ["You", "Eye"]
                  }
                }
              }) {
                nodes {
                  groupedBy {
                    name
                  }

                  aggregatedValues {
                    lengthInSeconds {
                      approximateAvg
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This query is case-sensitive (since there’s no ignoreCase: true) and only matches tracks with names that contain “You” and “Eye” such as In Your Eyes. The following tracks only contain “You” or “Eye” (but not both) and would not be returned:

  • Brown Eyed Girl
  • We Will Rock You
  • With or Without You

When searching on a single substring, anySubstringOf and allSubstringsOf behave the same, so feel free to use either.