Luence command-line demo

The Lucene command-line demo
First, you should download the latest Lucene distribution and then extract it to a working directory.
1.open a gnome-terminater

2.Setting your CLASSPATH,you need 4 jars like this ,

export LUENCE_HOME=/opt/lucene-6.0.1

export CLASSPATH= LUENCEDEMO/lib/luceneanalyzerscommon6.0.1.jar: LUENCEDEMO/lib/lucene-demo-6.0.1.jar: LUENCEDEMO/lib/lucenequeryparser6.0.1.jar: LUENCEDEMO/lib/lucene-core-6.0.1.jar

  1. make index,indexing files
    java org.apache.lucene.demo.IndexFiles -docs $LUENCE_HOME

  2. To search the index type:
    java org.apache.lucene.demo.SearchFiles

  3. type keywords what you will search after it

note:

IndexFiles.java: code to create a Lucene index.
SearchFiles.java: code to search a Lucene index.

java code test:
public static void main(String[] args) throws IOException, ParseException {

    Analyzer analyzer = new StandardAnalyzer();
    //create index writer configuration
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
    //store method 1:store in memory 
    // RAMDirectory dir = new RAMDirectory();

    //store method 2:store in fs 
    //specified store index directory
    Directory dir = NIOFSDirectory.open(Paths.get("/home/cypress/Test/LunceTest"));
    IndexWriter writer = new IndexWriter(dir, iwc);

    // make index
    ArrayList<Document> arrayList = new ArrayList<>();
    indexing(arrayList, "title", 1L, " I love you  forever");
    indexing(arrayList, "title", 2L, " will you love me ?");
    // writer.addDocument(document);
    writer.addDocuments(arrayList);
    writer.close();

    //for search
    // open the indexed directory just we have created
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
    // parse keyword 
    Query parse = new QueryParser("title", analyzer).parse("me");

    TopDocs res = searcher.search(parse, 5);
    System.out.println("Hits:" + res.totalHits);
    ScoreDoc[] hits = res.scoreDocs;
    for (ScoreDoc scoreDoc : hits) {
        Document doc = searcher.doc(scoreDoc.doc);
        System.out.println(doc.get("title") + "id:" + doc.get("id"));
    }
}

/**
 * <p>add document to collection ,for batching writer indexes and return it</p>
 * @param arrayList :add document to this collection
 * @param field: store field
 * @param id :data base record id
 * @param context :detail  of the record
 * @return
 */
private static ArrayList<Document> indexing(ArrayList<Document> arrayList, String field, Long id, String context) {
    Document document = new Document();
    document.add(new TextField(field, context, Store.YES));
    document.add(new StoredField("id", id));
    arrayList.add(document);
    return arrayList;
}

link: https://lucene.apache.org/core/6_0_1/demo/overview-summary.html#Setting_your_CLASSPATH

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值