Last active
June 11, 2017 15:33
-
-
Save masahitojp/de21574ae8af98fc27598f1055abaf68 to your computer and use it in GitHub Desktop.
apache lucene入門 第一章のプログラムをLucene6.5.xで動くようにしてみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
compile 'org.apache.lucene:lucene-analyzers-kuromoji:6.5.1' | |
compile group: 'org.apache.lucene', name: 'lucene-queryparser', version: '6.5.1' | |
testCompile 'junit:junit:4.12' | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.masahitojp; | |
/* | |
This program based on Apache Lucene入門 | |
*/ | |
import org.apache.lucene.analysis.Analyzer; | |
import org.apache.lucene.analysis.ja.JapaneseAnalyzer; | |
import org.apache.lucene.document.Document; | |
import org.apache.lucene.document.Field; | |
import org.apache.lucene.document.TextField; | |
import org.apache.lucene.index.DirectoryReader; | |
import org.apache.lucene.index.IndexWriter; | |
import org.apache.lucene.index.IndexWriterConfig; | |
import org.apache.lucene.queryparser.classic.ParseException; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.search.TopDocs; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.RAMDirectory; | |
import org.apache.lucene.queryparser.classic.QueryParser; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class HelloLucene { | |
private static final String FIELD_CONTENT = "content"; | |
private static final Directory directory = new RAMDirectory(); | |
private static final Analyzer analyzer = new JapaneseAnalyzer(); | |
private static final QueryParser qp = new QueryParser(FIELD_CONTENT, analyzer); | |
private static final String[] contents = { | |
"カツオはサザエの弟", "サザエはワカメの姉", "ワカメはカツオの妹", "カツオは長男" | |
}; | |
private static void makeIndex() { | |
try(final IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(analyzer))) { | |
for (final String content : contents) { | |
final Document doc = new Document(); | |
doc.add(new TextField(FIELD_CONTENT, content, Field.Store.YES)); | |
writer.addDocument(doc); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void searchIndex(final String q) { | |
try (final DirectoryReader ireader = DirectoryReader.open(directory)) { | |
final IndexSearcher searcher = new IndexSearcher(ireader); | |
Query query = qp.parse(q); | |
TopDocs hits = searcher.search(query,10); | |
System.out.println(Integer.toString(hits.totalHits) + " 件ヒットしました"); | |
for(int i =0;i < hits.totalHits; i++) { | |
int docId = hits.scoreDocs[i].doc; | |
Document doc = searcher.doc(docId); | |
System.out.println("\t" + doc.get(FIELD_CONTENT)); | |
} | |
} catch (IOException | ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
makeIndex(); | |
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){ | |
String q = null; | |
while(q==null || !q.equals("q")) { | |
System.out.println("\nInput query(if you input \"q\", this will quit) > " ); | |
System.out.flush(); | |
q=br.readLine(); | |
if(!q.equals("q")) { | |
searchIndex(q); | |
} | |
} | |
} | |
directory.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment