Reputation: 86747
I want to build a lucene index where each document value will have 4-5 words.
I then want to have some kind of fuzzy search for it. But at first, I'm just trying to match a simple 2-word mest phrase. But it does not work. What is wrong in the following example?
Directory index = new RAMDirectory();
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);
IndexWriter w = new IndexWriter(index, config);
Document doc = new Document();
doc.add(new TextField("name", "TEST ASD", Field.Store.YES));
w.addDocument(doc);
w.close();
FuzzyQuery q = new FuzzyQuery(new Term("name", "test asd"));
IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(index));
TopScoreDocCollector collector = TopScoreDocCollector.create(1, true); //only get the top scored doc
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
assertTrue(hits.length > 0); //FAILS
Upvotes: 2
Views: 558
Reputation: 9320
If you want to have match on exach two terms - you could use TextField, but with KeywordAnalyzer (http://lucene.apache.org/core/4_9_0/analyzers-common/org/apache/lucene/analysis/core/KeywordAnalyzer.html) (it's also will save exact data and won't tokenize it)
Upvotes: 1