Returning Specific Fields (Narrow Your Search)
Redis can have a large number of elements in a RediSearch document. You can limit your search queries to return only specific elements. (This is similar to the SQL Select col1, col2, col3 ... from.. statement).
In this example, we modify the code to return only the following fields:
customerID, customerName, address, orderID, productName
In this code fragment, we have hard-coded items that you will normally pass in as parameters.
Notes on the above code
- We create a class DynamicDoc to hold the returned record(s)
- Lines 17-19 specify the fields to return and add it to the query.
- The SearchResults are NOT cast to the CustomerOrders class used in the previous examples. Doing this could result in a significantly large number of unrequested null properties being returned to the caller. Instead, we cast the document record (Payload) to a Dictionary<string,object>
The returned records show only the requested fields in the payload; it looks like this;
The above query will return results for all the documents that match the the query. To restrict the query to specific documents keys we then make the following sample modifications to the code:
string[] returnFields = { "customerID", "customerName","address", "orderID", "productName" };
string[] limitKeys = { "northwind:orders_temp2063", "northwind:orders_temp2147" };
query.LimitKeys(limitKeys);
query.ReturnFields(returnFields);
query.WithPayloads = true;
Notes on above
- lines 2-4 are added to specify the document keys (ids)
- This query is then the equivalent of the SQL:
SELECT customerID, customerName, address, orderID, productName from tablename
where documentKey IN ('northwind:orders_temp2063', 'northwind:orders_temp2147')