Skip to content

Commit 4014d2d

Browse files
committed
format pom.xml
1 parent 3f04d96 commit 4014d2d

File tree

2 files changed

+83
-100
lines changed

2 files changed

+83
-100
lines changed

lucene-sample/pom.xml

+28-27
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,9 @@
88
<name>lucene-sample</name>
99

1010
<properties>
11+
<java.version>1.8</java.version>
1112
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1213
</properties>
13-
14-
<build>
15-
<plugins>
16-
<plugin>
17-
<groupId>org.codehaus.mojo</groupId>
18-
<artifactId>exec-maven-plugin</artifactId>
19-
<version>1.3.2</version>
20-
<executions>
21-
<execution>
22-
<id>ex</id>
23-
<phase>package</phase>
24-
<goals>
25-
<goal>exec</goal>
26-
</goals>
27-
<configuration>
28-
<executable>java</executable>
29-
<arguments>
30-
<argument>-classpath</argument>
31-
<classpath />
32-
<argument>HelloLucene.HelloLucene</argument>
33-
</arguments>
34-
</configuration>
35-
</execution>
36-
</executions>
37-
</plugin>
38-
</plugins>
39-
</build>
4014

4115
<dependencies>
4216
<dependency>
@@ -65,4 +39,31 @@
6539
<version>7.1.0</version>
6640
</dependency>
6741
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.codehaus.mojo</groupId>
47+
<artifactId>exec-maven-plugin</artifactId>
48+
<version>1.3.2</version>
49+
<executions>
50+
<execution>
51+
<id>ex</id>
52+
<phase>package</phase>
53+
<goals>
54+
<goal>exec</goal>
55+
</goals>
56+
<configuration>
57+
<executable>java</executable>
58+
<arguments>
59+
<argument>-classpath</argument>
60+
<classpath />
61+
<argument>HelloLucene.HelloLucene</argument>
62+
</arguments>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
</plugins>
68+
</build>
6869
</project>

lucene-sample/src/main/java/com/xingyun/HelloLucene.java

+55-73
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,67 @@
33
import java.io.IOException;
44
import java.text.ParseException;
55

6-
import org.apache.lucene.analysis.standard.StandardAnalyzer;
7-
import org.apache.lucene.document.Document;
8-
import org.apache.lucene.document.Field;
9-
import org.apache.lucene.document.StringField;
10-
import org.apache.lucene.document.TextField;
11-
import org.apache.lucene.index.DirectoryReader;
12-
import org.apache.lucene.index.IndexReader;
13-
import org.apache.lucene.index.IndexWriter;
14-
import org.apache.lucene.index.IndexWriterConfig;
15-
import org.apache.lucene.queryparser.classic.QueryParser;
16-
import org.apache.lucene.search.IndexSearcher;
17-
import org.apache.lucene.search.Query;
18-
import org.apache.lucene.search.ScoreDoc;
19-
import org.apache.lucene.search.TopScoreDocCollector;
20-
import org.apache.lucene.store.Directory;
21-
import org.apache.lucene.store.RAMDirectory;
22-
import org.apache.lucene.util.Version;
23-
246
public class HelloLucene {
257

268
public static void main(String[] args) throws IOException, ParseException {
279

2810
// 对于这个简单的例子,我们将从一些字符串中创建一个内存索引。
2911
// 0. Specify the analyzer for tokenizing text.
3012
// The same analyzer should be used for indexing and searching
31-
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
32-
33-
// 1. create the index
34-
Directory index = new RAMDirectory();
35-
36-
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
37-
38-
IndexWriter w = new IndexWriter(index, config);
39-
addDoc(w, "Lucene in Action", "193398817");
40-
addDoc(w, "Lucene for Dummies", "55320055Z");
41-
addDoc(w, "Managing Gigabytes", "55063554A");
42-
addDoc(w, "The Art of Computer Science", "9900333X");
43-
w.close();
44-
45-
// 2. query
46-
String querystr = args.length > 0 ? args[0] : "lucene";
47-
48-
// the "title" arg specifies the default field to use
49-
// when no field is explicitly specified in the query.
50-
Query q = null;
51-
try {
52-
q = new QueryParser(Version.LUCENE_40, "title", analyzer).parse(querystr);
53-
} catch (org.apache.lucene.queryparser.classic.ParseException e) {
54-
e.printStackTrace();
55-
}
56-
57-
// 3. search
58-
int hitsPerPage = 10;
59-
IndexReader reader = DirectoryReader.open(index);
60-
IndexSearcher searcher = new IndexSearcher(reader);
61-
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
62-
searcher.search(q, collector);
63-
ScoreDoc[] hits = collector.topDocs().scoreDocs;
64-
65-
// 4. display results
66-
System.out.println("Found " + hits.length + " hits.");
67-
for (int i = 0; i < hits.length; ++i) {
68-
int docId = hits[i].doc;
69-
Document d = searcher.doc(docId);
70-
System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
71-
}
72-
73-
// reader can only be closed when there
74-
// is no need to access the documents any more.
75-
reader.close();
76-
}
77-
78-
private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
79-
Document doc = new Document();
80-
doc.add(new TextField("title", title, Field.Store.YES));
81-
82-
// use a string field for isbn because we don't want it tokenized
83-
doc.add(new StringField("isbn", isbn, Field.Store.YES));
84-
w.addDocument(doc);
13+
// StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_7_1_0);
14+
//
15+
// // 1. create the index
16+
// Directory index = new RAMDirectory();
17+
//
18+
// IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
19+
//
20+
// IndexWriter w = new IndexWriter(index, config);
21+
// addDoc(w, "Lucene in Action", "193398817");
22+
// addDoc(w, "Lucene for Dummies", "55320055Z");
23+
// addDoc(w, "Managing Gigabytes", "55063554A");
24+
// addDoc(w, "The Art of Computer Science", "9900333X");
25+
// w.close();
26+
//
27+
// // 2. query
28+
// String querystr = args.length > 0 ? args[0] : "lucene";
29+
//
30+
// // the "title" arg specifies the default field to use
31+
// // when no field is explicitly specified in the query.
32+
// Query q = null;
33+
// try {
34+
// q = new QueryParser(Version.LUCENE_40, "title", analyzer).parse(querystr);
35+
// } catch (org.apache.lucene.queryparser.classic.ParseException e) {
36+
// e.printStackTrace();
37+
// }
38+
//
39+
// // 3. search
40+
// int hitsPerPage = 10;
41+
// IndexReader reader = DirectoryReader.open(index);
42+
// IndexSearcher searcher = new IndexSearcher(reader);
43+
// TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
44+
// searcher.search(q, collector);
45+
// ScoreDoc[] hits = collector.topDocs().scoreDocs;
46+
//
47+
// // 4. display results
48+
// System.out.println("Found " + hits.length + " hits.");
49+
// for (int i = 0; i < hits.length; ++i) {
50+
// int docId = hits[i].doc;
51+
// Document d = searcher.doc(docId);
52+
// System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
53+
// }
54+
//
55+
// // reader can only be closed when there
56+
// // is no need to access the documents any more.
57+
// reader.close();
58+
// }
59+
//
60+
// private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
61+
// Document doc = new Document();
62+
// doc.add(new TextField("title", title, Field.Store.YES));
63+
//
64+
// // use a string field for isbn because we don't want it tokenized
65+
// doc.add(new StringField("isbn", isbn, Field.Store.YES));
66+
// w.addDocument(doc);
67+
// }
8568
}
86-
8769
}

0 commit comments

Comments
 (0)