views:

293

answers:

1

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"....

+4  A: 

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);
}
itsadok
hi, thanks...using whitespaceanalyzer,how do i feed in stop words to lucene?
Steve Chapman
Basically, you have to write a new analyzer, which is not that hard. If you want more details, I suggest you open a new question.
itsadok