How to index semantically similar fields that have different names?

Last updated 22, Mar 2024

Question

How to index semantically similar fields that have different names?

Answer

As an example of indexing semantically similar fields in a document, if the keyspace is partitioned into two classes of keys, we may want to search by price and include variants of its name, like cost. Having this dataset:

JSON.SET prod:123 $ '{"name":"book","price":20}'
JSON.SET prod:124 $ '{"name":"chair","price":50}'
JSON.SET item:32 $ '{"name":"pen","cost":5}'

If cost and price are mutually exclusive, it is possible to create an index specifying what classes of documents are indexed with PREFIX and then create an index on both fields.

FT.CREATE idx ON JSON PREFIX 2 prod: item: SCHEMA $.name AS name TEXT "$.['price','cost']" AS price NUMERIC

FT.SEARCH idx '@name:(book|pen|chair) @price:[5 30]' RETURN 2 '$.price' '$.cost'
1) (integer) 2
2) "item:32"
3) 1) "$.cost"
   2) "5"
4) "prod:123"
5) 1) "$.price"
   2) "20"

References

Learn more about the query syntax to customize the search operations.