Filtering and Limiting Search Results

You can filter search results on the server side by applying a search filter – this is similar to the SQL where clause.

The example below demonstrates how to filter the query to return only results with the extendedPrice between 160 and 350.  It also introduces the query Limit method to return only the first 5 results – this is similar to the SELECT TOP  clause of SQL.

        [HttpGet]
        [Route("api/search/orders/filter")]
        public async Task<List<CustomerOrders>> SearchOrdersFilter()
        {
            string searchString = "Vins";
            IDatabase db = _connectionMultiplexer.GetDatabase();

            NRediSearch.Query query = new NRediSearch.Query(searchString);

            //filter results on extendedPrice between 160 and 350
            query.AddFilter(new NRediSearch.Query.NumericFilter("extendedPrice", 160, 350));            
            query.Limit(0, 5); //return only the first 5 documents
            query.WithPayloads = true;
            
            SearchResult res = await _rediSearchClient.SearchAsync(query);

		//the rest of the code(below) for processing the search results 
		//is the same as shown in the earlier example(s)

            List<CustomerOrders> customerOrdersList = new List<CustomerOrders>();
            foreach (Document doc in res.Documents)
            {
                IEnumerable<KeyValuePair<string, RedisValue>> record = doc.GetProperties();
                string jsonRecord = JsonConvert.SerializeObject(record);
                CustomerOrders custOrders = JsonConvert.DeserializeObject<CustomerOrders>(jsonRecord);
                custOrders.id = doc.Id;
                custOrders.score = doc.Score;
                customerOrdersList.Add(custOrders);
            }
            return customerOrdersList;
        }
Filtering/Limiting Search Results

The query above filters the query to return only results where the extendedPrice is between 160 and 350.  In addition to numeric filters, RediSearch also supports GEO filters; this is not covered here.
In line 12 we limit the returned records to the first 5.  The parameters for the Limit are the 0ffset and number of records  – the default is Limit(0,10).

Restricting Search Within Fields

The default behavior of RediSearch is to search all text properties specified in the schema.  You can restrict which fields to search as shown in the code fragment below.

         //search the customerID field for the text VINET 
          string searchString = "(@customerID:VINET)"; 
          
          //search the customerName field for wild-card text Suprê*
          string searchString = "(@customerName:Suprê*)";       
          
          //search for the above 2 texts.  -- this is a UNION operation
          string searchString = "(@customerName:Suprê*) | (@customerID:VINET)";   
Other Search Queries

RediSearch has several query examples at this link.