gpt4 book ai didi

java - Lucene-6.4.2 : No class found DefaultSimilarity, 尝试添加查询。

转载 作者:行者123 更新时间:2023-11-30 02:36:22 27 4
gpt4 key购买 nike

我正在我们的项目中测试 Apache lucene 以进行基于文本的搜索。不幸的是,我遇到了缺少库的问题。我尝试添加 lucene 查询,但这没有帮助。我做错了什么?

错误日志:

Caused by: java.lang.NoClassDefFoundError: org/apache/lucene/search/similarities/DefaultSimilarity
at org.hibernate.search.spi.SearchIntegratorBuilder.createCleanFactoryState(SearchIntegratorBuilder.java:287)
at org.hibernate.search.spi.SearchIntegratorBuilder.buildNewSearchFactory(SearchIntegratorBuilder.java:186)
at org.hibernate.search.spi.SearchIntegratorBuilder.buildSearchIntegrator(SearchIntegratorBuilder.java:117)
at org.hibernate.search.hcore.impl.HibernateSearchSessionFactoryObserver.sessionFactoryCreated(HibernateSearchSessionFactoryObserver.java:66)
at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:52)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:588)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 98 more
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.search.similarities.DefaultSimilarity
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
... 111 more

POM.xml:

  <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.4.2</version>
</dependency>

我正在尝试的代码:

  @Override
public void saveIndexes() {
//Apache Lucene Indexing Directory .txt files
try {
//indexing directory
Path path = Paths.get("/home/akshay/index/");
Directory directory = org.apache.lucene.store.FSDirectory.open(path);
IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.deleteAll();
File f = new File("/home/akshay/textfiles/"); // current directory
for (File file : f.listFiles()) {
System.out.println("indexed " + file.getCanonicalPath());
org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
doc.add(new TextField("path", file.getName(), Field.Store.YES));
FileInputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer stringBuffer = new StringBuffer();
String line;
while((line = reader.readLine())!=null){
stringBuffer.append(line).append("\n");
}
reader.close();
doc.add(new TextField("contents", stringBuffer.toString(), Field.Store.YES));
indexWriter.addDocument(doc);
}
indexWriter.close();
directory.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

@Override
public void searchLucene(String text) {
//Apache Lucene searching text inside .txt files
try {
Path path = Paths.get("/home/akshay/index/");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
QueryParser queryParser = new QueryParser("contents", new StandardAnalyzer());
Query query = queryParser.parse(text);
TopDocs topDocs = indexSearcher.search(query,10);
System.out.println("totalHits " + topDocs.totalHits);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println("path " + document.get("path"));
System.out.println("content " + document.get("contents"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

有什么想法吗,谢谢。 :-)

最佳答案

实际上,该类已在 5.4.1 中被弃用。 ,在6.4.2版本中查找时已不存在,查看消息:

Use ClassicSimilarity for equivilent behavior, or consider switching to BM25Similarity which will become the new default in Lucene 6.0

参见also :

  • LUCENE-6789: IndexSearcher's default Similarity is changed to BM25Similarity. Use ClassicSimilarity to get the old vector space DefaultSimilarity. (Robert Muir)

将 lucene 核心依赖项降级到 5.5.4,或者在代码中使用 ClassicSimilarityBM25Similarity

关于java - Lucene-6.4.2 : No class found DefaultSimilarity, 尝试添加查询。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42952846/

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