博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lucene
阅读量:5069 次
发布时间:2019-06-12

本文共 8003 字,大约阅读时间需要 26 分钟。

一、 package com.lucene.demo; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; 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.index.Term; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import java.io.IOException; import java.io.StringReader; import java.nio.file.Paths; public class Test01 {
public static void main(String[] args) {
addDoc(); //upDoc(); //delDoc(); search(); } public static void addDoc(){
try {
Analyzer analyzer = new SmartChineseAnalyzer(); Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter iwriter = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new TextField("id", "001", Field.Store.YES)); doc.add(new TextField("name","中华史", Field.Store.YES)); doc.add(new TextField("content","易中天的中华史", Field.Store.YES)); iwriter.addDocument(doc); iwriter.close(); System.out.println("*****************--新增--end**************************"); } catch (IOException e) {
e.printStackTrace(); } } public static void search(){
try {
Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); Analyzer analyzer = new SmartChineseAnalyzer(); DirectoryReader ireader = DirectoryReader.open(directory); IndexSearcher isearcher = new IndexSearcher(ireader); // Parse a simple query that searches for "text": QueryParser parser = new QueryParser("name", analyzer); Query query = parser.parse("中华"); ScoreDoc[] hits = isearcher.search(query, 10).scoreDocs; // Iterate through the results SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("", ""); Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query)); for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc); TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(hitDoc.get("name"))); String content = highlighter.getBestFragment(tokenStream, hitDoc.get("name")); System.out.println("id="+hitDoc.get("id")+"|name="+hitDoc.get("name")+"|content="+content); } ireader.close(); directory.close(); System.out.println("*****************--查询--end**************************"); } catch (Exception e) {
e.printStackTrace(); } } public static void upDoc(){
try {
Analyzer analyzer = new SmartChineseAnalyzer(); Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter iwriter = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new TextField("id", "001", Field.Store.YES)); doc.add(new TextField("name","中华史-近代史", Field.Store.YES)); doc.add(new TextField("content","易中天的中华史-近代史", Field.Store.YES)); Term term=new Term("id","001"); iwriter.updateDocument(term,doc); iwriter.close(); System.out.println("*****************--修改--end**************************"); } catch (IOException e) {
e.printStackTrace(); } } public static void delDoc(){
try {
Analyzer analyzer = new SmartChineseAnalyzer(); Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter iwriter = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new TextField("id", "001", Field.Store.YES)); doc.add(new TextField("name","中华史-近代史", Field.Store.YES)); doc.add(new TextField("content","易中天的中华史-近代史", Field.Store.YES)); Term term=new Term("id","001"); iwriter.deleteDocuments(term); iwriter.close(); System.out.println("*****************--删除---end**************************"); } catch (IOException e) {
e.printStackTrace(); } } } 二、pom.xml
4.0.0
com
lucene-01
1.0-SNAPSHOT
war
lucene-01 Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
org.apache.lucene
lucene-core
8.0.0
org.apache.lucene
lucene-queryparser
8.0.0
org.apache.lucene
lucene-highlighter
8.0.0
org.apache.lucene
lucene-analyzers-common
8.0.0
org.apache.lucene
lucene-analyzers
3.6.2
org.apache.lucene
lucene-queries
8.0.0
org.apache.lucene
lucene-memory
8.0.0
org.apache.lucene
lucene-analyzers-smartcn
8.0.0
lucene-01
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2

转载于:https://www.cnblogs.com/albert-think/p/10552707.html

你可能感兴趣的文章
flask简单应用以及配置文件的写法。
查看>>
n阶螺旋矩阵问题
查看>>
工作中碰到的一些问题以及解决方法
查看>>
C#-WinForm-对话框控件
查看>>
支持多个版本的ASP.NET Core Web API
查看>>
D - Xenia and Bit Operations
查看>>
tar用法
查看>>
Spring框架初识(二)
查看>>
redis设置密码
查看>>
【数据仓库】数据仓库概述
查看>>
form表单传对象
查看>>
中科燕园GIS外包--环境保护执法移动GIS平台
查看>>
Fibonacci博弈
查看>>
JSP中的九个内置对象(17)
查看>>
ContentProvider和Uri详解
查看>>
(转)ios学习--你会遇到的runtime面试题(详)
查看>>
redhat7.0安装postgresql
查看>>
【leetcode】482. License Key Formatting
查看>>
【Leetcode_easy】806. Number of Lines To Write String
查看>>
.NET assembly结构
查看>>