gpt4 book ai didi

java - 使用空格分析器搜索关键字

转载 作者:行者123 更新时间:2023-12-01 05:29:27 25 4
gpt4 key购买 nike

下面显示的是我为数据建立索引的方法:

public void getAvailableItems(String sql) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
IndexWriter writer=null;
File file = null;
try{
file = new File(LUCENE_INDEX_DIRECTORY);
analyzer = new WhitespaceAnalyzer(Version.LUCENE_CURRENT);
writer = new IndexWriter(
FSDirectory.open(file),
analyzer,
true,
IndexWriter.MaxFieldLength.LIMITED
);

Class.forName("com.mysql.jdbc.Driver").newInstance();
//get connection object
con = DriverManager.getConnection(
"jdbc:mysql://"+DB_HOST_NAME+"/evergoldbuilders", DB_USER_NAME, DB_PASSWORD);
//create statement object
stmt = con.createStatement();
//execute query
rs = stmt.executeQuery(sql);
//iterate through result set
while(rs.next()){
String name = rs.getString("category_name").trim() + " " + rs.getString("sub_category_name").trim() + " " + rs.getString("classification_name").trim() + " " + rs.getString("item_name").trim();

Document document = new Document();
Field nameField = new Field("item_complete_name", name, Field.Store.YES, Field.Index.ANALYZED);
document.add(nameField);
writer.addDocument(document);
}
writer.optimize();

}catch(Exception e){
e.printStackTrace();
}
}

以及我搜索关键字的方法:

public void searchItem(String column, String search)  throws Exception{

ScoreDoc[] hits = null;
QueryParser parser = null;
Query q = null;

int hitsPerPage = 50;
analyzer = new WhitespaceAnalyzer(Version.LUCENE_CURRENT);
File files = new File(LUCENE_INDEX_DIRECTORY);
IndexReader reader = IndexReader.open(FSDirectory.open(files),true);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
parser = new QueryParser(Version.LUCENE_CURRENT, "item_complete_name", analyzer);
q = parser.parse(search + "*");

searcher.search(q, collector);
hits = collector.topDocs().scoreDocs;

System.out.println("Found " + hits.length + " hits.");
count = 0;
for(int i=0;i<hits.length;++i) {
isFound = true;

int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println(d.getField("item_complete_name").stringValue());
count++;
}
searcher.close();
}

最后,我的样本数据被索引:

ALUMINUM  4'/O U.S. ALUMINUM
ALUMINUM 4" CHINA ALUMINUM
ALUMINUM 3'/O U.S. ALUMINUM
ALUMINUM 3"A CHINA ALUMINUM
PAINTS DAVIES 4 LITERS DV 472 HI-HEAT RESISTING ALUMINUM (1200°F)
PAINTS DAVIES 4 LITERS DV 470 SILVER FINISH ALUMINUM

我的问题是,每当我搜索“alum*”时,都找不到搜索结果,但找到“aluminum”。并且没有找到“aluminum AND china*”的搜索结果。我可以使用 lucene 通配符(即 * 和 ?)来使用空格分析器搜索索引数据吗?空格分析器是否标记非字母?我想要一个分析器来标记我的空间数据。空白分析器是否适合使用?非常感谢!

最佳答案

使用org.apache.lucene.analysis.standard.StandardAnalyzer代替WhitespaceAnalyzer可以解决问题。

关于java - 使用空格分析器搜索关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com