Full Text Search
Full Text Search
Couchbase Lite [1] supports Full Text Search (FTS).
FTS is accomplished using the match query.
FTS matches are case-insensitive.
In the Travel App, the FTS query is against local pre-built "travel-sample" database.
In order to do FTS queries, an FTS index must be created.
Open the fileapp/src/android/java/…/util/DatabaseManager.java.
We will review the createFTSQueryIndex() method.
This code snippet creates an FTS index on the property named description.
private void createFTSQueryIndex() {
try {
database.createIndex("descFTSIndex", IndexBuilder.fullTextIndex(FullTextIndexItem.property("description")));
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
Next you will write an FTS query that uses the index.
Open the fileapp/src/android/java/…/hotels/HotelsPresenter.java.
We will review the queryHotels(String location, String description) method.
@Override
public void queryHotels(String location, String description) {
...
}
First, you get an instance of the database.
Database database = DatabaseManager.getDatabase();
Next, you will create an FTS Expressions using the match() operator.
In this particular example, the match expression looks for the desciptionStr 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.
Expression descExp = FullTextFunction.match("descFTSIndex", description) ;
Expression locationExp = Expression.property("country")
.like(Expression.string("%" + location + "%"))
.or(Expression.property("city").like(Expression.string("%" + location + "%")))
.or(Expression.property("state").like(Expression.string("%" + location + "%")))
.or(Expression.property("address").like(Expression.string("%" + location + "%")));
Expression searchExp = descExp.and(locationExp);
Query hotelSearchQuery = QueryBuilder
.select(SelectResult.all())
.from(DataSource.database(database))
.where(
Expression.property("type").equalTo(Expression.string("hotel")).and(searchExp)
);
We build the query using the different expressions from above and transform the ResultSet object into a List<Map<String, Object>> object that is passed to the RecyclerView.
ResultSet rows = null;
try {
rows = hotelSearchQuery.execute();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
return;
}
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Result row = null;
while((row = rows.next()) != null) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("name", row.getDictionary("travel-sample").getString("name"));
properties.put("address", row.getDictionary("travel-sample").getString("address"));
data.add(properties);
}
mHotelView.showHotels(data);