首页 > 其他分享 >lucene 2.0中的一些要注意的地方

lucene 2.0中的一些要注意的地方

时间:2022-12-02 11:00:31浏览次数:27  
标签:java org lucene 注意 import apache new 2.0


lucene是一款不错的针对搞全文搜索的API,可以结合JAVA使用,但在用lucene 2.0时,如果参考目前的一些文章,可能会有一些API是过期了,我在看一些讲lucene的文章时,就遇到这类的情况,于是经过查找,发现有如下的一些要注意的地方。
Field.Text(java.lang.String, java.io.Reader)

new Field(java.lang.String, java.io.Reader)代替
Field.Keyword(java.lang.String, java.lang.String)

Field.Keyword(java.lang.String, java.lang.String)
代替

Query query = QueryParser.parse(q, "contents", new StandardAnalyzer ());

QueryParser parser = new QueryParser("contents", new StandardAnalyzer ());
Query query = parser.parse(q);

代替

因此,再摘录一个命令行方式下的简单lucene程序,来自IBM DW,效果是先对某目录下的TXT文件建立索引,然后再排序
建立索引的程序:

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

 

/**
 * This class demonstrate the process of creating index with Lucene
 * for text files
 */
public class TxtFileIndexer {
 public static void main(String[] args) throws Exception{
  //indexDir is the directory that hosts Lucene's index files
        File   indexDir = new File("D:\\luceneIndex");
        //dataDir is the directory that hosts the text files that to be indexed
        File   dataDir  = new File("D:\\luceneData");
        Analyzer luceneAnalyzer = new StandardAnalyzer();
        File[] dataFiles  = dataDir.listFiles();
        IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
        long startTime = new Date().getTime();
        for(int i = 0; i < dataFiles.length; i++){
         if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){
          System.out.println("Indexing file " + dataFiles[i].getCanonicalPath());
          Document document = new Document();
          Reader txtReader = new FileReader(dataFiles[i]);
          

          document.add(new Field("path",dataFiles[i].getPath(),Field.Store.YES,Field.Index.NO));
          document.add(new Field("contents",txtReader));
         
          indexWriter.addDocument(document);
         }
        }
        indexWriter.optimize();
        indexWriter.close();
        long endTime = new Date().getTime();
       
        System.out.println("It takes " + (endTime - startTime)
                           + " milliseconds to create index for the files in directory "
                     + dataDir.getPath());       
 }
}

搜索的程序
import java.io.File;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;

/**
 * This class is used to demonstrate the
 * process of searching on an existing
 * Lucene index
 *
 */
public class TxtFileSearcher {
 public static void main(String[] args) throws Exception{
     String queryStr = "我";
     //This is the directory that hosts the Lucene index
        File indexDir = new File("D:\\luceneIndex");
        FSDirectory directory = FSDirectory.getDirectory(indexDir,false);
        IndexSearcher searcher = new IndexSearcher(directory);
        if(!indexDir.exists()){
         System.out.println("The Lucene index is not exist");
         return;
        }
        Term term = new Term("contents",queryStr.toLowerCase());
        TermQuery luceneQuery = new TermQuery(term);
        Hits hits = searcher.search(luceneQuery);
        System.out.println("his result is"+hits.length());
        for(int i = 0; i < hits.length(); i++){
         Document document = hits.doc(i);
         System.out.println("File: " + document.get("path"));
        }
 }
}

 

标签:java,org,lucene,注意,import,apache,new,2.0
From: https://blog.51cto.com/u_14230175/5906249

相关文章

  • Log4Net使用注意事项
    重点关注.config文件的配置配置configSections<configSections><sectionname="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/></c......
  • C# 调用系统软键盘帮助类(兼容.netframework2.0)
    前言最近再做触屏系统的时候需要手动调用打开系统软键盘的需求,网上查找到的资料很多,在高版本的fx上也能用,但是刚好我这个现场程序是基于fx2.0开发的,只能在之前的基础上改造......
  • C#如何判断操作系统位数(32/64),兼容.netframework2.0
    前言最近在做项目的时候遇到之前老系统维护的时候发现需要判断操作系统版本的业务,之前的系统是基于.netframework2.0,微软在4.0之后才提供了快捷查询操作系统位数的函数,只能......
  • 【重要】数据库操作注意事项!!!
    1.数据库操作一定要谨慎谨慎再谨慎!!!!!,必须加上where条件,哪怕就是全部改,也得加上where1=1;!!!2.如果SQL要操作的表的开发内容不是自己负责,则一定不要自己去写SQL,让相关开发提供SQ......
  • 多态注意事项
    多态注意事项:1、多态是方法的多态,属性没有多态2、父类和子类,有联系没有联系会类型转换异常!ClassCastException!3、父类和子类同时存在同名的方法,会调用子类的方法4......
  • Mysql在数据应用中的注意事项
    1. 前言1.1.背景● 数据库被广泛应用:各类业务系统、信息化系统,数据仓库、数据分析、数据挖掘。● 数据使用中存在的常见问题。● 了解基本、强制的使用规范,有助于更好的......
  • 搜索引擎之Lucene,Solr,ElasticSearch比较
    目录1搜索引擎1.1简介1.2结构化数据和非结构化数据1.3使用全文搜索引擎条件2Lucene,Solr,ElasticSearch2.1Lucene2.2Solr2.3ElasticSearch2.4区别和选择2.4.1如何......
  • 不支持PowerShell 2.0版本(don't support PowerShell version 2.0. )
    在“程序包管理器控制台”使用命令“update-database”会提示:TheEntityFrameworkCorePackageManagerConsoleToolsdon'tsupportPowerShellversion2.0.Upgradet......
  • torch.autograd.Function 用法及注意事项
    众所周知,作为深度学习框架之一的PyTorch和其他深度学习框架原理几乎完全一致,都有着自动求导机制,当然也可以说成是自动微分机制。有些时候,我们不想要它自带的求导机制,需要......
  • 基于云开发的答题活动小程序v2.0-首页设计与实现
    项目技术栈微信原生小程序+云开发。为什么选择微信原生小程序进行开发呢?因为能够直接应用它的云开发能力吖。我这里主要使用了云开发能力中的小程序端SDK,说白了就是在jav......