java实现中文模糊查询(附完整源码)
时间: 2023-11-11 21:04:24 浏览: 124
java模糊查询案例(附源码)
以下是Java实现中文模糊查询的完整源码,使用了Lucene作为搜索引擎:
```java
import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
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.FSDirectory;
import org.apache.lucene.util.Version;
public class ChineseFuzzySearch {
private static final String INDEX_DIR = "index";
private static final String[] FIELDS = { "title", "content" };
private static Analyzer analyzer = new SimpleAnalyzer(Version.LUCENE_4_9);
public static void main(String[] args) throws IOException, ParseException {
// 创建索引
createIndex();
// 执行查询
String queryStr = "中国人民银行";
TopDocs docs = search(queryStr, 10);
System.out.println("搜索结果:");
for (ScoreDoc sd : docs.scoreDocs) {
Document d = getDocument(sd.doc);
System.out.println(d.get("title") + " -> " + d.get("content"));
}
}
/**
* 创建索引
*/
public static void createIndex() throws IOException {
Directory dir = FSDirectory.open(Paths.get(INDEX_DIR));
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(dir, config);
// 添加文档
addDoc(writer, "中国人民银行", "中华人民共和国的中央银行");
addDoc(writer, "中国银行", "中华人民共和国的一家大型商业银行");
addDoc(writer, "中国工商银行", "中华人民共和国的一家大型商业银行");
addDoc(writer, "中国建设银行", "中华人民共和国的一家大型商业银行");
writer.close();
}
/**
* 添加文档
*/
private static void addDoc(IndexWriter writer, String title, String content) throws IOException {
Document doc = new Document();
doc.add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
}
/**
* 执行查询
*/
public static TopDocs search(String queryStr, int num) throws IOException, ParseException {
Directory dir = FSDirectory.open(Paths.get(INDEX_DIR));
DirectoryReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
// 多字段查询
MultiFieldQueryParser parser = new MultiFieldQueryParser(FIELDS, analyzer);
Query query = parser.parse(queryStr);
// 模糊查询
query = query.rewrite(reader);
// 执行查询
TopDocs docs = searcher.search(query, num);
reader.close();
return docs;
}
/**
* 根据文档ID获取文档
*/
public static Document getDocument(int docID) throws IOException {
Directory dir = FSDirectory.open(Paths.get(INDEX_DIR));
DirectoryReader reader = DirectoryReader.open(dir);
Document doc = reader.document(docID);
reader.close();
return doc;
}
}
```
该程序首先创建一个索引,然后执行模糊查询,最后输出查询结果。在查询时,程序使用了Lucene自带的 `MultiFieldQueryParser` 进行多字段查询,并对查询关键字进行了模糊匹配。
阅读全文