Index terms
When you create an index you have the option of including one or more
entries in the terms
field. Each entry represents a field name to make
searchable in the index’s source collection(s).
Let’s add some documents to a collection named cars
:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
[
{
ref: Ref(Collection("cars"), "1"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Civic', doors: 4 }
},
{
ref: Ref(Collection("cars"), "2"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Accord', doors: 2 }
},
{
ref: Ref(Collection("cars"), "3"),
ts: 1645226205590000,
data: { make: 'Toyota', model: 'Camry', doors: 4 }
}
]
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
In order to search values in the make
field, we can include make
in
the terms
definition in an index called cars_by_make
:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
ref: Index("cars_by_make"),
ts: 1641940850400000,
active: true,
serialized: true,
name: "cars_by_make",
source: Collection("cars"),
terms: [
{
field: ["data", "make"]
}
],
partitions: 1
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
Note that if any new documents are added to the cars
collection which
do not include a make
field, those documents are not represented in
the cars_by_make
index.
Now we can use the cars_by_make
index to search for documents which
match on search criteria for the make
field:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
data: [
{
ref: Ref(Collection("cars"), "1"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Civic', doors: 4 }
},
{
ref: Ref(Collection("cars"), "2"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Accord', doors: 2 }
}
]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
If you need your application to support searching by multiple fields,
you can create an index with multiple fields in the terms
definition.
The following example creates an index on the make
and model
fields
in the cars
collection:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
ref: Index("cars_by_make_and_model"),
ts: 1641943522140000,
active: true,
serialized: true,
name: "cars_by_make_and_model",
source: Collection("cars"),
terms: [
{ field: ["data", "make"] },
{ field: ["data", "model"] }
],
partitions: 1
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
Queries against an index with multiple terms must include an array of
search criteria. The criteria are compared against the fields in the
terms
field in the order in which they are listed. The following
example searches for documents in the cars
collection which match
Toyota
in the make
field and Camry
in the model
field:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
data: [
{
ref: Ref(Collection("cars"), "3"),
ts: 1645226205590000,
data: { make: 'Toyota', model: 'Camry', doors: 4 }
}
]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!