Lucene

http://lucene.apache.org/core/3_6_1/demo.html

CLASSPATH

  • OK
    • export CLASSPATH=/home/mac/xxxx/xxx/xxx.jar:/home/mac/yyyy/yyy/yyy.jar
    • export CLASSPATH=/home/mac/xxxx/xxx/*:/home/mac/yyyy/yyy/*
  • Not OK
    • export CLASSPATH=/home/mac/xxxx/xxx/*.jar:/home/mac/yyyy/yyy/*.jar
    • export CLASSPATH=/home/mac/xxxx/xxx/:/home/mac/yyyy/yyy/

http://lucene.apache.org/core/3_6_1/demo2.html

  • Need to detect doc language and change to use correct analyzer.
Create Index
  • open an directory to put index files (dir)
  • new an Analyzer (analyzer)
  • new an IndexWriterConfig (iwc)
  • do some settings on IndexWriterConfig
  • use dir and iwc to new a IndexWriter (writer)
  • add documents
    • new a Document (doc)
    • add several fields
      • new a Field (pathField)
        • Field pathField = new Field("path", file.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
        • pathField.setIndexOptions(IndexOptions.DOCS_ONLY);
        • doc.add(pathField);
      • new a NumericField (modifiedField)
        • NumericField modifiedField = new NumericField("modified");
        • modifiedField.setLongValue(file.lastModified());
        • doc.add(modifiedField);
      • Add real content by reading actual file
        • doc.add(new Field("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8"))));
  • Add / Update index
    • Add: writer.addDocument(doc);
    • Update: writer.updateDocument(new Term("path", file.getPath()), doc);
  • close writer
Search
  • Create an IndexReader by assigning a directory (reader)
  • new a IndexSearcher by assign reader (searcher)
  • new an Analyzer (analyzer)
  • new a QueryParser (parser) // need to indicate which field will be searched.
    • QueryParser parser = new QueryParser(Version.LUCENE_31, field, analyzer);
  • create Query (query)
    • Query query = parser.parse(line)
  • do search
    • Normal search
      • searcher.search(query, null, 100); // get top 100 hits. (null is for filter)
    • Do search with paging
      • There is a simple sample code in the demo source

留言

這個網誌中的熱門文章

買車記

怎麼在兩台linux server間用scp而不需打密碼?

Costco退貨真爽快