|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 |
| -var java = require("../../"); |
| 3 | +const java = require("../../"); |
4 | 4 | java.classpath.push("./lucene-lib/lucene-core-7.4.0.jar");
|
5 | 5 | java.classpath.push("./lucene-lib/lucene-analyzers-common-7.4.0.jar");
|
6 | 6 | java.classpath.push("./lucene-lib/lucene-queryparser-7.4.0.jar");
|
7 | 7 |
|
| 8 | +const idx = java.newInstanceSync("org.apache.lucene.store.RAMDirectory"); |
| 9 | +const analyzer = java.newInstanceSync("org.apache.lucene.analysis.standard.StandardAnalyzer"); |
| 10 | +const writerConfig = java.newInstanceSync("org.apache.lucene.index.IndexWriterConfig", analyzer); |
| 11 | +const writer = java.newInstanceSync("org.apache.lucene.index.IndexWriter", idx, writerConfig); |
| 12 | +const queryParser = java.newInstanceSync("org.apache.lucene.queryparser.classic.QueryParser", "content", analyzer); |
8 | 13 |
|
9 |
| -var idx = java.newInstanceSync("org.apache.lucene.store.RAMDirectory"); |
10 |
| -var analyzer = java.newInstanceSync("org.apache.lucene.analysis.standard.StandardAnalyzer"); |
11 |
| -var writerConfig = java.newInstanceSync("org.apache.lucene.index.IndexWriterConfig", analyzer); |
12 |
| -var writer = java.newInstanceSync("org.apache.lucene.index.IndexWriter", idx, writerConfig); |
13 |
| -var queryParser = java.newInstanceSync("org.apache.lucene.queryparser.classic.QueryParser", "content", analyzer); |
14 |
| - |
15 |
| -writer.addDocumentSync(createDocument("Theodore Roosevelt", |
16 |
| - "It behooves every man to remember that the work of the " + |
17 |
| - "critic, is of altogether secondary importance, and that, " + |
18 |
| - "in the end, progress is accomplished by the man who does " + |
19 |
| - "things.")); |
20 |
| -writer.addDocumentSync(createDocument("Friedrich Hayek", |
21 |
| - "The case for individual freedom rests largely on the " + |
22 |
| - "recognition of the inevitable and universal ignorance " + |
23 |
| - "of all of us concerning a great many of the factors on " + |
24 |
| - "which the achievements of our ends and welfare depend.")); |
25 |
| -writer.addDocumentSync(createDocument("Ayn Rand", |
26 |
| - "There is nothing to take a man's freedom away from " + |
27 |
| - "him, save other men. To be free, a man must be free " + |
28 |
| - "of his brothers.")); |
29 |
| -writer.addDocumentSync(createDocument("Mohandas Gandhi", |
30 |
| - "Freedom is not worth having if it does not connote " + |
31 |
| - "freedom to err.")); |
| 14 | +writer.addDocumentSync( |
| 15 | + createDocument( |
| 16 | + "Theodore Roosevelt", |
| 17 | + "It behooves every man to remember that the work of the " + |
| 18 | + "critic, is of altogether secondary importance, and that, " + |
| 19 | + "in the end, progress is accomplished by the man who does " + |
| 20 | + "things." |
| 21 | + ) |
| 22 | +); |
| 23 | +writer.addDocumentSync( |
| 24 | + createDocument( |
| 25 | + "Friedrich Hayek", |
| 26 | + "The case for individual freedom rests largely on the " + |
| 27 | + "recognition of the inevitable and universal ignorance " + |
| 28 | + "of all of us concerning a great many of the factors on " + |
| 29 | + "which the achievements of our ends and welfare depend." |
| 30 | + ) |
| 31 | +); |
| 32 | +writer.addDocumentSync( |
| 33 | + createDocument( |
| 34 | + "Ayn Rand", |
| 35 | + "There is nothing to take a man's freedom away from " + |
| 36 | + "him, save other men. To be free, a man must be free " + |
| 37 | + "of his brothers." |
| 38 | + ) |
| 39 | +); |
| 40 | +writer.addDocumentSync( |
| 41 | + createDocument("Mohandas Gandhi", "Freedom is not worth having if it does not connote " + "freedom to err.") |
| 42 | +); |
32 | 43 |
|
33 | 44 | writer.closeSync();
|
34 | 45 |
|
35 |
| -var searcher = java.newInstanceSync("org.apache.lucene.search.IndexSearcher", java.callStaticMethodSync("org.apache.lucene.index.DirectoryReader", "open", idx)); |
| 46 | +const searcher = java.newInstanceSync( |
| 47 | + "org.apache.lucene.search.IndexSearcher", |
| 48 | + java.callStaticMethodSync("org.apache.lucene.index.DirectoryReader", "open", idx) |
| 49 | +); |
36 | 50 |
|
37 | 51 | search(searcher, "freedom");
|
38 | 52 | search(searcher, "free");
|
39 | 53 | search(searcher, "progress or achievements");
|
40 | 54 |
|
41 | 55 | function createDocument(title, content) {
|
42 |
| - var fieldStoreYes = java.callStaticMethodSync("org.apache.lucene.document.Field$Store", "valueOf", "YES"); |
43 |
| - var doc = java.newInstanceSync("org.apache.lucene.document.Document"); |
| 56 | + const fieldStoreYes = java.callStaticMethodSync("org.apache.lucene.document.Field$Store", "valueOf", "YES"); |
| 57 | + const doc = java.newInstanceSync("org.apache.lucene.document.Document"); |
44 | 58 | doc.addSync(java.newInstanceSync("org.apache.lucene.document.TextField", "title", title, fieldStoreYes));
|
45 | 59 | doc.addSync(java.newInstanceSync("org.apache.lucene.document.TextField", "content", content, fieldStoreYes));
|
46 | 60 | return doc;
|
47 | 61 | }
|
48 | 62 |
|
49 | 63 | function search(searcher, queryString) {
|
50 |
| - var query = queryParser.parseSync(queryString); |
51 |
| - var topDocs = searcher.searchSync(query, 10); |
52 |
| - |
53 |
| - console.log("Found " + topDocs.totalHits + " hits for query \"" + queryString + "\"."); |
54 |
| - var scoreDocs = topDocs.scoreDocs; |
55 |
| - for(var i=0; i<topDocs.scoreDocs.length; i++) { |
56 |
| - var docId = scoreDocs[i].doc; |
57 |
| - var doc = searcher.docSync(docId); |
| 64 | + const query = queryParser.parseSync(queryString); |
| 65 | + const topDocs = searcher.searchSync(query, 10); |
| 66 | + |
| 67 | + console.log("Found " + topDocs.totalHits + ' hits for query "' + queryString + '".'); |
| 68 | + const scoreDocs = topDocs.scoreDocs; |
| 69 | + for (let i = 0; i < topDocs.scoreDocs.length; i++) { |
| 70 | + const docId = scoreDocs[i].doc; |
| 71 | + const doc = searcher.docSync(docId); |
58 | 72 | console.log(" " + (i + 1) + ". " + doc.getSync("title"));
|
59 | 73 | }
|
60 | 74 | }
|
0 commit comments