实验四 构建索引(2学时)
1、实验目的和要求
题目二:修改题目一的代码,使用多值域在一个文档中存放3本书的书名值。
题目三:针对题目一的三个文档,分别做如下操作:根据书名在索引中删除一个值、修改一个文档的域值。
4、实验实现过程
package com.GreatIndex;import java.io.File;
import java.io.IOException;
import java.util.Dictionary;import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldSelectorResult;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;import ba.se.impl.util.Version;public class GreatIndex {public static void main(String[] args) {GreatIndex GreatIndexObj = new GreatIndex();GreatIndex.setUp();GreatIndex.DeleteDocument();GreatIndex.UpdateDocument();GreatIndex.setUp2();} static String indexDir = "E:/learnspa/yidongsousuosuanfa/workspace/lab-index";static FSDirectory directory;
// IndexWriter name = new IndexWriter(dictionary,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFiledLength.UNLIMITED);private static void setUp() {try {directory = FSDirectory.open(new File(indexDir));IndexWriter writer = new IndexWriter(directory,new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);Document doc1 = new Document();Document doc2= new Document();Document doc3= new Document();doc1.add(new Field("bookname","伐清",org.apache.lucene.document.Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));doc2.add(new Field("bookname","奥术神座",org.apache.lucene.document.Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));doc3.add(new Field("bookname","冰与火之歌",org.apache.lucene.document.Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));writer.addDocument(doc1);writer.addDocument(doc2);writer.addDocument(doc3);writer.close();System.out.println("setUpDocument"+doc1);System.out.println("setUpDocument"+doc2);System.out.println("setUpDocument"+doc3);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private static void DeleteDocument() {// TODO Auto-generated method stubtry {IndexWriter writer = new IndexWriter(directory,new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
// 使用优化策略直接删除文档writer.deleteDocuments(new Term("bookname","伐清"));writer.close();System.out.println("DeleteDocument"+new Document());} catch (CorruptIndexException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (LockObtainFailedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private static void UpdateDocument() {// TODO Auto-generated method stubtry {IndexWriter writer = new IndexWriter(directory,new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
// 创建一个新的document用于替换Document document = new Document();document.add(new Field("bookname", "lucene实战第二版",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));writer.updateDocument(new Term("booknmae","官仙"), document);writer.close();System.out.println("UpdateDocument"+document);} catch (CorruptIndexException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (LockObtainFailedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
static String[] booknames = {"伐清","奥术神座","冰与火之歌"};
private static void setUp2() {// TODO Auto-generated method stubtry {directory = FSDirectory.open(new File(indexDir));IndexWriter writer = new IndexWriter(directory,new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);Document doc = new Document();for (String bookname : booknames) {doc.add(new Field("bookname", bookname,Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));writer.addDocument(doc);
// writer.close();System.out.println("setUp2"+doc);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
}
}
实验结果
本文发布于:2024-01-29 13:05:47,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170650475015490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |