Reputation: 263
My first try with Lucene.Net
I have an Index with 500 Documents (html and pdf) with some fields like url, content, title
All works fine when im searching in content and/or title
But when i search for an Url i got no results
So i found url like "/tlfdi/epapers/datenschutz2016/files/assets/common/downloads/page0004.pdf" but not "page0004.pdf"
Also with "*" its not working.
Index and Search uses the WhitespaceAnalyzer. With StandardAnalyzer i got zero results when i search for "/kontakt/index.aspx"
Search:
WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30,
new[] { "url", "title", "description", "content", "keywords" }, analyzer);
Query query = parseQuery(searchQuery, parser);
Lucene.Net.Search.ScoreDoc[] hits = (Lucene.Net.Search.ScoreDoc[])searcher.Search(
query, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
Someone can help?
Upvotes: 1
Views: 1299
Reputation: 4963
The standard fare of analyzers won't do what you want, instead, you'll have to write a custom tokenizer and analyzer.
It's easy! We just need to scratch out a tokenizer and an analyzer.
The UrlTokenizer is responsible for generating tokens.
// UrlTokenizer delimits tokens by whitespace, '.' and '/'
using AttributeSource = Lucene.Net.Util.AttributeSource;
public class UrlTokenizer : CharTokenizer
{
public UrlTokenizer(System.IO.TextReader @in)
: base(@in)
{
}
public UrlTokenizer(AttributeSource source, System.IO.TextReader @in)
: base(source, @in)
{
}
public UrlTokenizer(AttributeFactory factory, System.IO.TextReader @in)
: base(factory, @in)
{
}
//
// This is where all the magic happens for our UrlTokenizer!
// Whitespace, forward slash or a period are a token boundary.
//
protected override bool IsTokenChar(char c)
{
return !char.IsWhiteSpace(c) && c != '/' && c != '.';
}
}
The UrlAnalyzer consumes input streams and implement UrlTokenizer and the LowerCaseFilter for case insensitive searches.
// Custom Analyzer implementing UrlTokenizer and LowerCaseFilter.
public sealed class UrlAnalyzer : Analyzer
{
public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
{
//
// This is where all the magic happens for UrlAnalyzer!
// UrlTokenizer token text are filtered to lowercase text.
return new LowerCaseFilter(new UrlTokenizer(reader));
}
public override TokenStream ReusableTokenStream(System.String fieldName, System.IO.TextReader reader)
{
Tokenizer tokenizer = (Tokenizer)PreviousTokenStream;
if (tokenizer == null)
{
tokenizer = new UrlTokenizer(reader);
PreviousTokenStream = tokenizer;
}
else
tokenizer.Reset(reader);
return tokenizer;
}
}
SO here's code demonstrating the UrlAnalyzer. I replaced the MultiFieldQueryParser with QueryParser for clarity.
//
// Demonstrate UrlAnalyzer using an in memory index.
//
public static void testUrlAnalyzer()
{
string url = @"/tlfdi/epapers/datenschutz2016/files/assets/common/downloads/page0004.pdf";
UrlAnalyzer analyzer = new UrlAnalyzer();
Directory directory = new RAMDirectory();
QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "url", analyzer);
IndexWriter writer = new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(2048));
//
// index a document. We're only interested in the "url" field of a document.
//
Document doc = new Document();
Field field = new Field("url", url, Field.Store.NO, Field.Index.ANALYZED);
doc.Add(field);
writer.AddDocument(doc);
writer.Commit();
//
// search the index for any documents having 'page004.pdf' in the url field.
//
string searchText = "url:page0004.pdf";
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = parser.Parse(searchText);
ScoreDoc[] hits = searcher.Search(query, null, 10, Sort.RELEVANCE).ScoreDocs;
if (hits.Length == 0)
throw new System.Exception("RamblinRose is fail!");
//
// search the index for any documents having the full URL we indexed.
//
searchText = "url:\"" + url + "\"";
query = parser.Parse(searchText);
hits = searcher.Search(query, null, 10, Sort.RELEVANCE).ScoreDocs;
if (hits.Length == 0)
throw new System.Exception("RamblinRose is fail!");
}
Lucene.net is good stuff. I hope this code improves your understanding of Lucene analysis.
Good luck!
p.s. beware wildcard searches to solve problems, it's a real killer on large indexes.
Upvotes: 3