Home:ALL Converter>Searching using Apache Lucene

Searching using Apache Lucene

Ask Time:2014-06-23T23:19:00         Author:user3766749

Json Formatter

I am using Apache lucene to search for a string within a file . What kind of parsing of does lucene use. If I search for obama it doesn't return results with Presobama while it returns results for #Obama. Can anyone tell me why? I am using TextField.

         StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);

        //  Code to create the index
        Directory index = new RAMDirectory();

        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);

        IndexWriter w = new IndexWriter(index, config);
        addDoc(w, finalstep);

        w.close();
                    String querystr =  search;

        //  The \"title\" arg specifies the default field to use when no field is explicitly specified in the query
        Query q = new QueryParser(Version.LUCENE_44, "title", analyzer).parse(querystr);

        // Searching code
        int hitsPerPage = 10;
        IndexReader reader = DirectoryReader.open(index);
        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        searcher.search(q, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;

Author:user3766749,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24369540/searching-using-apache-lucene
yy