gpt4 book ai didi

Java、Lucene : Case insensitive search not working with WildCard query

转载 作者:行者123 更新时间:2023-12-02 02:48:14 27 4
gpt4 key购买 nike

我正在致力于在基于 Spring-MVC 的应用程序中集成 Lucene 搜索和索引,目前我正在使用通配符查询搜索,但它不能以不区分大小写的方式工作。有任何想法吗?

当我输入 Ideas 时,我会得到结果,但不是创意。

代码:

@Override
public synchronized void saveIndexes(String text, String tagFileName, String filePath, long groupId, boolean type, int objectId) {
try {
Directory directory = org.apache.lucene.store.FSDirectory.open(path);
IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
IndexWriter indexWriter = new IndexWriter(directory, config);
org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
if (filePath != null) {
File file = new File(filePath); // current directory
doc.add(new TextField("path", file.getPath(), Field.Store.YES));
}
doc.add(new StringField("id", String.valueOf(objectId), Field.Store.YES));
// doc.add(new TextField("id",String.valueOf(objectId),Field.Store.YES));
if (text == null) {
if (filePath != null) {
FileInputStream is = new FileInputStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuffer.append(line).append("\n");
}
stringBuffer.append("\n").append(tagFileName);
if(groupNotes!=null){
stringBuffer.append(String.valueOf(groupNotes.getNoteNumber()));
}
reader.close();
doc.add(new TextField("contents", stringBuffer.toString(), Field.Store.YES));
}
}

}

//搜索代码

 @Override
public List<Integer> searchLucene(String text, long groupId, boolean type) {
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Query query = new WildcardQuery(new Term("contents","*"+text + "*"));
TopDocs topDocs = indexSearcher.search(query, 50);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
objectIds.add(Integer.valueOf(document.get("id")));
System.out.println("Text is "+document.get("contents"));
}
indexSearcher.getIndexReader().close();
directory.close();
}

谢谢。

最佳答案

我会尝试将小写文本存储到索引中。当用户搜索文本时,将搜索查询文本设置为小写,然后再在索引中搜索。

https://stackoverflow.com/a/30926385/4587961

关于Java、Lucene : Case insensitive search not working with WildCard query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44237850/

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