Full Text Search
Full Text Search
Couchbase Lite 2.0 supports Full Text Search (FTS). FTS is accomplished using the match
query.
FTS matches are case-senstive.
In the Travel App, the FTS query is against local "travel-sample" documents that is pre-built with the app.
In order to do FTS queries, an FTS index must be created.
Open the fileHotelsDao.java
.
We will review the searchHotelsAsync()
method.
This code snippet creates an FTS index on the property named description
.
@Nonnull
private List<Hotel> searchHotelsAsync(@Nonnull String location, @Nonnull String desc) {
...
}
final ResultSet results = QueryBuilder
.select(SelectResult.expression(Meta.id), SelectResult.all())
.from(DataSource.database(db.getDatabase()))
.where(Expression.property(DbManager.PROP_DOC_TYPE).equalTo(Expression.string(Hotel.DOC_TYPE))
.and(FullTextExpression.index(DbManager.FTS_INDEX_DESC).match(desc)
.and(Expression.property(Hotel.PROP_ADDRESS).like(Expression.string(loc))
.or(Expression.property(Hotel.PROP_CITY).like(Expression.string(loc)))
.or(Expression.property(Hotel.PROP_STATE).like(Expression.string(loc)))
.or(Expression.property(Hotel.PROP_COUNTRY).like(Expression.string(loc))))))
.orderBy(Ordering.property(Hotel.PROP_NAME).ascending())
.execute();
This is a fairly involved query expression.
- You will create an FTS Expressions
using the match()
operator. In this particular example, the match
expression looks for the desc
value in the description
property.
- This match
expression is logically ANDed with an equalTo
comparison expression which looks for the location
in the country
,city
,state
or address
properties.
- This expression is then used in the where
clause of the query the usual way.
We build the query using the different expressions from above and parse the ResultSet
object into a List<Hotel>
object
for (Result result : results.allResults()) {
if (result.count() < 2) { continue; }
final Hotel hotel = Hotel.fromDictionary(result.getString(0), result.getDictionary(1));
if (hotel != null) { hotels.add(hotel); }
}
Try it out
-
Log into the Travel Sample Mobile app as “demo” user and password as “password”
-
Tap on "hotels" button
-
In the description text field enter “Pets”.
-
In the Location text field enter "London" (Note the search is case sensitive)
-
Verify that you see one hotel listed named "Novotel London West"
