0
votes

Am newbie to Lucene.

Is there any way I can make Lucene analyzer not ignore dots in the string?? for example,if my search criteria is: "A.B.C.D",Lucene should give me only those documents in the search results which have "A.B.C.D" and not "ABCD"....

1

1 Answers

5
votes

It's all about the analyzer you use. The StandardAnalyzer does some complicated things with dotted names, in an attempt to "Do What You Mean". Perhaps the WhitespaceAnalyzer will be a better match for your needs.

public static void main(String[] args) throws Exception {
    RAMDirectory dir = new RAMDirectory();
    IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
    Document doc = new Document();
    doc.add(new Field("text", "A.B.C.D DEF", Field.Store.YES, Field.Index.ANALYZED));
    iw.addDocument(doc);
    iw.close();

    IndexSearcher searcher = new IndexSearcher(dir);
    QueryParser queryParser = new QueryParser("text", new WhitespaceAnalyzer());

    // prints 0 
    System.out.println(searcher.search(queryParser.parse("ABCD"), 1).totalHits);

    // prints 1
    System.out.println(searcher.search(queryParser.parse("A.B.C.D"), 1).totalHits);
}