Skip to main content

Search Documents

Tigris offers a realtime search for documents in a collection. Searching is by default enabled for a collection, there is no setup. This guide section will walk through how to use Tigris search in different scenarios.

Example collection

Let's first have the collection.

TigrisCollection<Catalog> catalog = tigrisDatabase.getCollection(Catalog.class);

Assuming an e-commerce website that has the above collection catalog and has 6 products(documents) in it.

idnamepricebrandlabelspopularityreviews
1fiona handbag99.9michael korspurses8{"author": "alice", "rating": 7}
2tote bag49coachhandbags9{"author": "olivia", "rating": 8.3}
3sling bag75coachpurses9{"author": "alice", "rating": 9.2}
4sneakers shoes40adidasshoes10{"author": "olivia", "rating": 9}
5running shoes89nikeshoes10{"author": "olivia", "rating": 8.5}
6running shorts35adidasclothing7{"author": "olivia", "rating": 7.5}

Searching for documents

Search consists of executing a query against one or more text fields. Let's perform a simple search query to lookup any items matching "running".

SearchRequest request = SearchRequest.newBuilder().withQuery("running").build();
Iterator<SearchResult<Catalog>> results = catalog.search(request);
info

Search is case insensitive i.e. a search for term "ruNninG" would match all of ["Running", "running", "RUnnIng", "RUNNING"] etc.

By default, search is performed over individual terms in the text. For example, search for a query string adventure park in a dataset would yield following results:

  1. "California's kids adventure park and Safari"
  2. "Adventure island and water park"
  3. "Long Island water park and adventure activities"
  4. "Six flags kids recreation and adventure park"
  5. "Hollywood adventure park and studios"
  6. "Seaworld adventure and theme park"

The search phrase can be escaped in query for exact match. In the above example, querying for exact phrase \"adventure park\" would return:

  1. "California's kids adventure park and Safari"
  2. "Six flags kids recreation and adventure park"
  3. "The Great America adventure park and Zoo"

Phrases can still be combined with keywords for richer text search. Continuing above example, the query string kids \"adventure park\" would result in:

  1. "California's kids adventure park and Safari"
  2. "Six flags kids recreation and adventure park"
SearchRequest request = SearchRequest.newBuilder().withQuery("\"adventure park\"").build();

SearchRequest request = SearchRequest.newBuilder().withQuery("kids \"adventure park\"").build();

Match all search query

When query string isn't specified or an empty string (""), a match all query is performed. It returns all searchable documents, modified by any filters or search parameters used.

SearchRequest request = SearchRequest.matchAll().build();
tip

Returning all documents is typically useful when used in conjunction with filter, or when performing a faceted search across the collection.

Project search query against specific fields

We can optionally project the search query against selected fields. Continuing previous example of searching for "running", we may not want to search in reviews field and avoid any unwanted results.

SearchRequest request =
SearchRequest.newBuilder()
.withQuery("running")
.withSearchFields("name", "labels")
.build();

Refine the search results using filters

Let's adjust the query to only return items in price range of [40, 90). We can use filters in search to further refine the results.

SearchRequest request =
SearchRequest.newBuilder()
.withQuery("running")
.withSearchFields("name", "labels")
.withFilter(
Filters.and(
Filters.eq("price", 40), Filters.eq("price", 90)))
.build();

Exact text match

Filters can be used to match against one or more field values in a collection. For example, to fetch all items from brand "adidas".

SearchRequest request =
SearchRequest.matchAll()
.withFilter(Filters.eq("brand", "adidas"))
.build();

We can additionally retrieve the number of items a particular brand has and unique labels, that match our search query.

SearchRequest request =
SearchRequest.newBuilder()
.withQuery("running")
.withSearchFields("name", "labels")
.withFilter(
Filters.and(
Filters.gte("price", 40), Filters.lt("price", 90)))
.withFacetFields("brand", "labels")
.build();

Facets are a specific use-case of filters, and can only be used for filterable attributes.

Faceted content navigation UI

Common application for faceted search is to build UX with quick filters, that users can use to narrow search results in real-time. Faceted search interface presents intuitive content navigation to the end user.

Sorting the search results

Tigris lets you specify an order to sort the search results. We can specify a ranking order in our search query to have results sorted with more popular items appearing first.

SearchRequest request =
SearchRequest.newBuilder()
.withQuery("running")
.withSearchFields("name", "labels")
.withSort(Sort.descending("popularity"))
.build();

Many documents may have the same popularity score, we can specify additional user-defined sortable field to break the tie.

SearchRequest request =
SearchRequest.newBuilder()
.withQuery("running")
.withSearchFields("name", "labels")
.withSort(Sort.descending("popularity"), Sort.descending("reviews.rating"))
.build();

The results will be first sorted by value of popularity field, reviews.rating will be used to decide ordering if two matching documents have same popularity.

note

Documents can only be sorted by integer, number and date-time type of collection fields.

Specifying document fields to retrieve

Search query can be programmed to return only specific fields in a document in search results. We may only need to retrieve product name, brand and price for our interface.

SearchRequest request =
SearchRequest.newBuilder()
.withQuery("running")
.withIncludeFields("name", "brand", "price")
.build();

On the contrary, exclusion of fields is useful to exclude/hide potentially sensitive fields or internal metadata from the document. To include all fields except id and reviews from documents in search results.

SearchRequest request =
SearchRequest.newBuilder()
.withQuery("running")
.withExcludeFields("id", "reviews")
.build();
note

Field selection does not impact searching, filtering and faceting capabilities for that field. For example, if reviews field is not included in documents in search results, it could still be used for text querying, filtering and/or faceting; just that matched documents won't include reviews field.

Case Insensitive Search Result Filtering

Search is case-insensitive but the filtering to restrict the search result is case-sensitive by default. Tigris supports Collation which allows you to specify string comparison rules for filtering on text fields. Set the case to ci in the collation object to make it case-insensitive. The following example is showing when you are searching for text "running" and you need to filter by brand field, but you don't care about the case.