What’s a shingle in Lucene parlance?
Join the DZone community and get the full member experience.Join For Free
every now and then we get asked what the heck is a shingle in lucene, as in the shinglefilter or the shinglematrixfilter, so it seems worthwhile to provide some info on shingles in lucene, solr and lucidworks enterprise . first off, a shingle is just a word-based n-gram, as opposed to a character-based n-gram (ngramtokenizer, ngramtokenfilter, edgengramtokenizer and edgengramtokenfilter provide the latter functionality). we named it shingles just to differentiate the two when it comes to naming the filters and, well, because like shingles on your roof, they overlap each other.
what are shingles good for? many people use them to create “pseudo-phrases” during the indexing process since the shingle ends up being a single token, which is then subject to the normal tf-idf scoring that is used in lucene. in many cases, searching for phrases yields relevance improvements, but finding phrases at query-time can be more expensive than normal term queries, so people sometimes try to get ahead of the game and use shingles.
if you want to see shingles in action and compare them to n-grams, add the following field types to a sample solr schema:
<filter class="solr.shinglefilterfactory" minshinglesize="2" maxshinglesize="5"/>
<tokenizer class="solr.ngramtokenizerfactory" maxgramsize="5" mingramsize="2"/>
next start up your solr instance and browse to http://localhost:8983/solr/admin/analysis.jsp and do the following steps:
- in the field row, choose “type” from the dropdown and enter shingle in the text area
- in the field value section, choose verbose output and enter “the quick red fox jumped over the lazy brown dogs”.
- hit submit. you should see something like:
as you can see, there are multiple tokens put out for each position, many of which contain multiple words as a single token.
next, try the same sentence, but switch from “shingle” to “ngram” for the field type. this time you should see the words split up into character groups.
Opinions expressed by DZone contributors are their own.