How to find related items by tags in Lucene.NET
My indexed documents have a field containing a set of IDs:
a845497737704e8ab439dd410e7f1328|
0a2d7192f75148cca89b6df58fcf2e54|
204fce58c936434598f7bd7eccf11771
(ignore line breaks)
This field is a list of tags. The list can contain tags 0 to n .
When users of my site are viewing a specific document, I want to display a list of related documents. This list of related documents should be identified by tags:
- Only documents with at least one matching tag should appear in the list of matching documents.
- The document with the best matching tags should appear at the top of the list of related documents.
I thought to use WildcardQuery for this, but queries starting with '*' are not allowed.
Any suggestions?
a source to share
Putting aside possible use of Lucene for this task for a moment (which I'm not too familiar with), consider checking the LinkDatabase.
Sitecore will track all of your links to and from items behind the scenes. And since your multiple tags are indeed (I assume) picked from the meta-hierarchy of tags provided somewhere by Sitecore - LinkDatabase will be able to tell you all the elements that link to it.
In some pseudocode layout, it would then
for each ID in tags
get all documents referencing this tag
for each document found
if master-list contains document; increase usage-count
else; add document to master list
sort master-list by usage-count descending
Forgive me for not being more precise, but not mock up a fully working example right at this point.
You can find an article on LinkDatabase here http://larsnielsen.blogspirit.com/tag/XSLT . Keep in mind that if you tag documents using the TreeListEx field, there is a known flaw in early versions of Sitecore. Documented here: http://www.cassidy.dk/blog/sitecore/2008/12/treelistex-not-registering-links-in.html
a source to share
Try this query in the tag field.
+(tag1 OR tag2 OR ... tagN)
where tag1, .. tagN are document tags.
This query will return documents with at least one tag match. Automatic scoring will take care of the documents with the most matches, since the final score is the sum of the individual points.
Also, you need to understand that if you want to find documents that are similar to Doc1 tags, you will find Doc1 at the top of the search results. So, handle this case accordingly.
a source to share