I run a digital music store with a "single search" that weights keywords based on their occurrences and the schema in which Products appear, eg. with different columns like "Artist", "Title" or "Publisher".
Products are also related to albums and playlists, but for simpler explanation, I will only elaborate on the indexing and querying of Products' Keywords.
Database Schema
Keywords
table - a weighted table for every word that could possibly be searched for (hence, it is referenced somewhere) with the following data for each record:
- Keyword ID (not the word),
- The Word itself,
- A Soundex Alpha value for the Word
- Weight
ProductKeywords
table - a weighted table for every keyword referenced by any of a product's fields (or columns) with the following data for each record:
- Product ID,
- Keyword ID,
- Weight
Keyword Weighting
The weighting value is an indication of how often the words occurs. Matching keywords with a lower weight are "more unique" and are more likely to be what is being searched for. In this way, words occurring often are automatically "down-weighted", eg. "the", "a" or "I". However, it is best to strip out atomic occurrences of those common words before indexing.
I used integers for weighting, but using a decimal value will offer more versatility, possibly with slightly slower sorting.
Indexing
Whenever any product field is updated, eg. Artist or Title (which does not happen that often), a database trigger re-indexes the product's keywords like so inside a transaction:
- All product keywords are disassociated and deleted if no longer referenced.
- Each indexed field (eg. Artist) value is stored/retrieved as a keyword in its entirety and related to the product in the
ProductKeywords
table for a direct match.
- The keyword weight is then incremented by a value that depends on the importance of the field. You can add, subtract weight based on the importance of the field. If Artist is more important than Title, Subtract 1 or 2 from its
ProductKeyword
weight adjustment.
- Each indexed field value is stripped of any non-alphanumeric characters and split into separate word groups, eg. "Billy Joel" becomes "Billy" and "Joel".
- Each separate word group for each field value is soundexed and stored/retrieved as a keyword and associated with the product in the same way as in step 2. If a keyword has already been associated with a product, its weight is simply adjusted.
Querying
- Take the input query search string in its entirety and look for a direct matching keyword. Retrieve all ProductKeywords for the keyword in an in-memory table along with Keyword weight (different from ProductKeyword weight).
- Strip out all non-alphanumeric characters and split query into keywords. Retrieve all existing keywords (only a few will match). Join ProductKeywords to matching keywords to in-memory table along with Keyword weight, which is different from the ProductKeyword weight.
- Repeat Step 2 but use soundex values instead, adjusting weights to be less relevant.
- Join retrieved ProductKeywords to their related Products and retrieve each product's sales, which is a measure of popularity.
- Sort results by Keyword weight, ProductKeyword weight and Sales. The final summing/sorting and/or weighting depends on your implementation.
- Limit results and return product search results to client.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…