Filters
Engine Type | Supported? |
---|---|
Crawler-based Engine | YES |
API-based Engine | YES |
Filters apply conditions to matching document fields to reduce a result set.
- q
- required
- The query used within your search.
- engine_key
- required
- The Engine Key associated with your Engine. Found within your dashboard.
- filters
- optional
- Provide the
filters
parameter with a DocumentType key, and then provide that key with fields from your schema. To filter, provide the fields with a value to filter upon.
Basic Filtering
Filter by matching on a specific value.
books
DocumentType that are "in_stock":"true"
and "genre":"fiction"
only. Crawler based Engines use page
as the default DocumentType.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "brothers",
"filters":{
"books":{
"genre":"fiction",
"in_stock":"true"
}
}
}'
Multiple Value Filters
Filters can also have multiple values.
mystery
OR science fiction
genres.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "brothers",
"filters":{
"books":{
"genre": ["mystery", "science fiction"]
}
}
}'
Filter Negation
You may want to filter out a particular field value.
For that, you can use the !
operator.
mystery
genre
.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "brothers",
"filters":{
"books":{
"genre": ["!mystery"]
}
}
}'
Filters with AND Semantics
By default, if you pass multiple values to a filter we apply OR semantics to the match.
To apply AND semantics, you may use the AND filter.
- type
- optional
- The type of filter. Within the field being filtered, provide the type as
and
. - values
- optional
- Within the field being filtered, provide the values to
and
between.
brothers
that are in both the mystery AND science fiction genres.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "brothers",
"filters":{
"books":{
"genre":{
"type": "and",
"values": ["mystery","science fiction"]
}
}
}
}'
Geospatial Queries
Geospatial queries require latitude and longitude coordinates.
Units can be miles (mi
) or kilometers (km
).
- type
- optional
- The type of filter. Within the field being filtered, provide the type as
distance
. - max
- optional
- Toggle the amount of distance from your specific coordinates wherein results are considered within proximity. Requires latitude and longitude.
- lat
- optional
- The latitude of the location. Requires longitude.
- lon
- optional
- The longitude of the location. Requires latitude.
5km
of the geographic location (37.75
,-122.42
).
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "brothers",
"filters":{
"books":{
"publisher_location":{
"type": "distance",
"max": "5km",
"lat": 37.75,
"lon": -122.42
}
}
}
}'
Range Queries
Filters also support range queries.
Apply range filters to date
, integer
, and float
fields.
If you want the filter unbounded at the low or the high end, just leave off the from
or the to
parameter.
- type
- optional
- The type of filter. Within the field being filtered, provide the type as
range
. - from
- optional
- The bottom end of the range, where the range begins.
- to
- optional
- The top end of the range, where the range ends.
3.00
and 12.00
.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
-H 'Content-Type: application/json' \
-d '{
"engine_key": "YOUR_ENGINE_KEY",
"q": "brothers",
"filters":{
"books":{
"price":{
"type": "range",
"from": 3.00,
"to": 12.00
}
}
}
}'
Supported Field Types
Type | Filtering |
---|---|
string |
Yes |
text |
No |
enum |
Yes |
integer |
Yes |
float |
Yes |
date |
Yes |
location |
Yes |
Stuck? Looking for help? Contact support or check out the Site Search community forum!