yns
yns

Reputation: 450

How to use Lucene IndexReader to read index in version 4.4?

For the just the sake of learning I've created an index from 1 file and wanted to search it. I am using Lucene Version 4.4. I know that indexing part is true.

tempFileName is the name of file which contains tokens and this file has the following words :

"odd plus odd is even ## even plus even is even ## odd plus even is odd ##"

However when I provide a query it returns nothing. I can't see what would be the problem. Any help is greatly appreciated.

Indexing part :

public void startIndexingDocument(String indexPath) throws IOException {
        Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_44);
        SimpleFSDirectory directory = new SimpleFSDirectory(new File(indexPath));
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44,
                analyzer);
        IndexWriter writer = new IndexWriter(directory, config);
        indexDocs(writer);
        writer.close();
    } 

    private void indexDocs(IndexWriter w) throws IOException {
        Document doc = new Document();
        File file = new File(tempFileName);

        BufferedReader br = new BufferedReader(new FileReader(tempFileName));
        Field field = new StringField(fieldName, br.readLine().toString(),
                Field.Store.YES);
        doc.add(field);
        w.addDocument(doc);
    }

Searching part :

public void readFromIndex(String indexPath) throws IOException,
            ParseException {
        Analyzer anal = new WhitespaceAnalyzer(Version.LUCENE_44);
        QueryParser parser = new QueryParser(Version.LUCENE_44, fieldName, anal);
        Query query = parser.parse("odd");
        IndexReader reader = IndexReader.open(NIOFSDirectory.open(new File(
                indexPath)));
        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
        searcher.search(query, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        // display
        System.out.println("fieldName =" + fieldName);
        System.out.println("Found : " + hits.length + " hits.");
        for (int i = 0; i < hits.length; i++) {
            int docId = hits[i].doc;
            Document d = searcher.doc(docId);
            System.out.println((i + 1) + ". " + d.get(fieldName));
        }
        reader.close();

    }

Upvotes: 0

Views: 2930

Answers (2)

acet
acet

Reputation: 1

StringField have a single token. So, I try to test with simple code.

for example @yns~ If you have a file that this is cralwer file and this contents hava a single String.

ex) file name : data03.scd , contents : parktaeha

You try to search with "parktaeha" queryString.

You get the search result! field name : acet, queryString parktaeha

======== start search!! ========== q=acet:parktaeha Found 1 hits. result array length :1 search result=> parktaeha ======== end search!! ==========

Look under the code. This code is test code.

    while((target = in.readLine()) != null){
        System.out.println("target:"+target);
        doc.add(new TextField("acet",target ,Field.Store.YES));  // use TextField                                                        
        // TEST : doc.add(new StringField("acet", target.toString(),Field.Store.YES));
    }

ref url

Upvotes: 0

femtoRgon
femtoRgon

Reputation: 33351

The problem is that you are using a StringField. StringField indexes the entire input as a single token. Good for atomic strings, like keywords, identifiers, stuff like that. Not good for full text searching.

Use a TextField.

Upvotes: 2

Related Questions