在这个基础上,添加对输入的关键词进行搜索
时间: 2024-02-06 18:12:51 浏览: 27
好的,以下是修改后的Java代码,用于实现一个基于Lucene的全文搜索引擎界面,并支持输入关键词进行搜索:
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.ParseException;
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.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
public class SearchEngineGUI extends JFrame implements ActionListener {
private JTextField searchField;
private JTextArea resultArea;
public SearchEngineGUI() {
super("Search Engine");
// 设置窗口大小和布局
setPreferredSize(new Dimension(500, 400));
setLayout(new BorderLayout());
// 创建搜索框和搜索按钮
searchField = new JTextField(30);
JButton searchButton = new JButton("Search");
JPanel searchPanel = new JPanel();
searchPanel.add(searchField);
searchPanel.add(searchButton);
// 创建搜索结果显示区域
resultArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(resultArea);
// 把搜索框和搜索结果显示区域添加到窗口中
add(searchPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
// 添加搜索按钮的点击事件监听器
searchButton.addActionListener(this);
// 显示窗口
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new SearchEngineGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
try {
// 创建一个内存索引库
Directory indexDirectory = new RAMDirectory();
// 创建一个索引写入器
IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
IndexWriter writer = new IndexWriter(indexDirectory, config);
// 添加一些文档到索引库中
addDocument(writer, "1", "Lucene is a full-text search engine library in Java.");
addDocument(writer, "2", "It is very fast and highly scalable.");
addDocument(writer, "3", "Lucene provides advanced features like fuzzy search and proximity search.");
// 关闭索引写入器
writer.close();
// 创建一个查询器
IndexSearcher searcher = new IndexSearcher(indexDirectory);
QueryParser parser = new QueryParser("content", new StandardAnalyzer());
// 搜索文档
String queryString = searchField.getText();
if (queryString.isEmpty()) {
resultArea.setText("Please enter a search keyword.");
} else {
Query query = parser.parse(queryString);
TopDocs results = searcher.search(query, 10);
// 输出搜索结果
if (results.totalHits == 0) {
resultArea.setText("No results found for \"" + queryString + "\".");
} else {
resultArea.setText("Search Results for \"" + queryString + "\":\n");
for (ScoreDoc hit : results.scoreDocs) {
Document document = searcher.doc(hit.doc);
resultArea.append(document.get("id") + " " + document.get("content") + "\n");
}
}
}
// 关闭查询器和索引库
searcher.getIndexReader().close();
indexDirectory.close();
} catch (IOException | ParseException ex) {
ex.printStackTrace();
}
}
// 添加文档到索引库中
private static void addDocument(IndexWriter writer, String id, String content) throws IOException {
Document document = new Document();
IndexableField idField = new StringField("id", id, Field.Store.YES);
IndexableField contentField = new StringField("content", content, Field.Store.YES);
document.add(idField);
document.add(contentField);
writer.addDocument(document);
}
}
```
这个示例在原有的基础上,添加了对输入的关键词进行搜索的功能。当用户点击搜索按钮时,程序会先判断搜索框中是否有关键词,如果没有则提示用户输入关键词,否则搜索包含关键词的文档,并在搜索结果显示区域中显示搜索结果。如果没有搜索到任何结果,则在搜索结果显示区域中提示用户。