What search can you build into your GraphQL API? Dgraph builds search into the fields of each type, so searching is available at deep levels in a query
@search directive tells Dgraph what search to build into your GraphQL API.
When a type contains an @search directive, Dgraph constructs a search input
type and a query in the GraphQL Query type. For example, if the schema
contains
queryPost GraphQL query for querying posts. The
@search directives in the Post type control how Dgraph builds indexes and
what kinds of search it builds into queryPost. If the type contains
title field
name) and return only their posts that
contain the term “GraphQL”.
| argument | constructed filter |
|---|---|
| none | lt, le, eq, in, between, ge, and gt |
Int, Float and DateTime is enabled by adding
@search to the field with no arguments. For example, if a schema contains:
numLikes in two ways: a query for
posts and field search on any post list.
A field queryPost is added to the Query type of the schema.
PostFilter will contain less than lt, less than or equal to le, equal
eq, in list in, between range between, greater than or equal to ge, and
greater than gt search on numLikes. Allowing for example:
posts, with
| argument | constructed filters |
|---|---|
year, month, day, or hour | lt, le, eq, in, between, ge, and gt |
@search with no arguments, DateTime also allows specifying how
the search index should be built: by year, month, day or hour. @search
defaults to year, but once you understand your data and query patterns, you
might want to changes that like @search(by: [day]).
| argument | constructed filter |
|---|---|
| none | true and false |
isPublished: Boolean @search
is in the schema, then the search allows
@search.
| argument | constructed searches |
|---|---|
hash | eq and in |
exact | lt, le, eq, in, between, ge, and gt (lexicographically) |
regexp | regexp (regular expressions) |
term | allofterms and anyofterms |
fulltext | alloftext and anyoftext |
hash and exact can’t be used together./ and
/. For example, query for “Diggy” and anyone else with “iggy” in their name:
anyofterms: "GraphQL tutorial" would match posts with either “GraphQL” or
“tutorial”.
fulltext search is Google-stye text search with stop words, stemming. etc. So
alloftext: "run woman" would match “run” as well as “running”, etc. For
example, to find posts that talk about fantastic GraphQL tutorials:
eq and regular expressions, add both options to the type
definition, as follows.
| argument | constructed searches |
|---|---|
| none | eq and in |
hash | eq and in |
exact | lt, le, eq, in, between, ge, and gt (lexicographically) |
regexp | regexp (regular expressions) |
@search with no arguments is the
same as @search(by: [hash]) and provides eq and in searches. Also
available for enums are exact and regexp. For hash and exact search on
enums, the literal enum value, without quotes "...", is used, for regexp,
strings are required. For example:
GraphQL tag.
While @search(by: [exact, regexp] would also admit lt etc. and
Point, Polygon and MultiPolygon. All of
them are searchable.
The following table lists the generated filters for each type when you include
@search on the corresponding field:
| type | constructed searches |
|---|---|
Point | near, within |
Polygon | near, within, contains, intersects |
MultiPolygon | near, within, contains, intersects |
Hotel type that has a location and an area:
near filter matches all entities where the location given by a field is
within a distance meters from a coordinate.
within filter matches all entities where the location given by a field is
within a defined polygon.
contains filter matches all entities where the Polygon or MultiPolygon
field contains another given point or polygon.
point or polygon can be taken inside the ContainsFilter at a
time.contains example using point:
contains example using polygon:
intersects filter matches all entities where the Polygon or
MultiPolygon field intersects another given polygon or multiPolygon.
polygon or multiPolygon can be given inside the
IntersectsFilter at a time.order argument. The results will be ordered
by the uid of each node in ascending order.members union field
in the Home type with filters and pagination.
null values for a
filter will query all members.@search directive is used in conjunction with @embedding directive to
define the HNSW index on vector embeddings. These vector embeddings are obtained
from external Machine Learning models.
name_v is an embedding on which the HNSW algorithm
is used to create a vector search index.
The metric used to compute the distance between vectors (in this example) is
Euclidean distance. Other possible metrics are cosine and dotproduct.
The directive, @embedding, designates one or more fields as vector embeddings.
The exponent value is used to set reasonable defaults for HNSW internal tuning
parameters. It is an integer representing an approximate number for the vectors
expected in the index, in terms of power of 10. Default is “4” (10^4 vectors).