简体   繁体   English

Lucene-6.4.2:找不到类DefaultSimilarity,尝试添加查询。

[英]Lucene-6.4.2 : No class found DefaultSimilarity, tried adding queries.

I am working on testing Apache lucene for text based search in our project. 我正在为我们的项目中的基于文本的搜索测试Apache lucene。 Unfortunately, I am having problem with missing libraries. 不幸的是,我在缺少库时遇到了问题。 I tried adding the lucene-queries, but that didn't help. 我尝试添加lucene查询,但这没有帮助。 What am I doing wrong? 我究竟做错了什么?

ErrorLog : 错误日志:

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 : 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>

Code I am trying : 我正在尝试的代码:

  @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();
        }
    }

Any ideas, thank you. 任何想法,谢谢。 :-) :-)

Actually the class was deprecated already in 5.4.1 , when looking for it in the 6.4.2 version it does not exist anymore, see the message: 实际上,该类已在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 使用ClassicSimilarity进行同等行为,或考虑切换到BM25Similarity,这将成为Lucene 6.0中的新默认设置

See also :

  • LUCENE-6789: IndexSearcher's default Similarity is changed to BM25Similarity. LUCENE-6789:IndexSearcher的默认“相似性”更改为BM25“相似性”。 Use ClassicSimilarity to get the old vector space DefaultSimilarity. 使用ClassicSimilarity获取旧的向量空间DefaultSimilarity。 (Robert Muir) (罗伯特·缪尔)

Either downgrade your lucene core dependency to 5.5.4, or use in your code either ClassicSimilarity or BM25Similarity 将您的Lucene核心依赖项降级为5.5.4,或者在您的代码中使用ClassicSimilarityBM25Similarity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM