用Lucene创建索引

创建maven项目pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.pactera</groupId>
    <artifactId>pactera-lucene</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>4.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>4.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>4.10.2</version>
        </dependency>
        <dependency>
            <groupId>cn.itcast.lucene.analyzer</groupId>
            <artifactId>ik-analyzer</artifactId>
            <version>2012-4.x</version>
        </dependency>
    </dependencies>
</project>

测试

//测试创建索引
    @Test
    public void testIndexWriter() throws IOException{
        //创建索引目录
        Directory directory = FSDirectory.open(new File("d:\\directory"));

        //创建标准分词器
        Analyzer analyzer = new StandardAnalyzer();
        //索引配置
        IndexWriterConfig indexWriterConfig = 
                new IndexWriterConfig(Version.LUCENE_4_10_2, analyzer);
        indexWriterConfig.setOpenMode(OpenMode.CREATE);
        //写索引
        IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
        //创建文档对象
        Document doc = new Document();
        doc.add(new IntField("id", 18, Store.YES));
        doc.add(new TextField("title", "我们都是党的接班人yes or no?", Store.YES));
        doc.add(new LongField("price", 6388L, Store.YES));
        doc.add(new StringField("pic", "www.baidu.com", Store.YES));

        //添加文档
        indexWriter.addDocument(doc);
        indexWriter.commit();
        indexWriter.close();
    }

在指定索引目录下查看索引
这里写图片描述

这种文件可以通过两种方式查看
第一种使用工具
这里写图片描述

用工具打开指定的目录就可以看到是怎么创建索引的
这里写图片描述

使用标准分词器汉字按单个字全部被拆分了

用lucene提供的TokenStream查看

@Test
    public void testTokenStream() throws IOException{
        //创建标准分词器
        Analyzer analyzer = new StandardAnalyzer();
        //词汇列表
        TokenStream tokenStream = analyzer.tokenStream("title", "我们都是党的接班人yes or no?");

        //tokenStream指针指向开始位置
        tokenStream.reset();

        //设置分词偏移量引用
        OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);

        //设置分词词语引用
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);

        //遍历词汇列表
        while(tokenStream.incrementToken()){
            //分词开始位置
            System.out.println("分词开始位置:" + offsetAttribute.startOffset());
            //分词词语
            System.out.println("最小分词单元:" + charTermAttribute);
            //分词结束位置
            System.out.println("分词结束位置:" + offsetAttribute.endOffset());
        }
    }

结果
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C# Lucene.Net创建索引的步骤: 1.添加Lucene.Net库的引用 在Visual Studio中,右键单击项目并选择“管理NuGet程序包”。在搜索框中搜索“Lucene.Net”,然后安装Lucene.Net库。 2.创建索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Store; using System.IO; // 创建索引 public void CreateIndex(string indexPath, string dataPath) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引写入器 var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); // 读取数据文件 var lines = File.ReadAllLines(dataPath); // 遍历数据文件中的每一行 foreach (var line in lines) { // 创建文档 var doc = new Document(); // 添加字段 doc.Add(new Field("content", line, Field.Store.YES, Field.Index.ANALYZED)); // 将文档写入索引 writer.AddDocument(doc); } // 关闭索引写入器 writer.Dispose(); } ``` 3.使用索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.QueryParsers; using Lucene.Net.Search; using Lucene.Net.Store; using System.IO; // 使用索引 public void SearchIndex(string indexPath, string queryStr) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引搜索器 var searcher = new IndexSearcher(directory, true); // 创建查询解析器 var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "content", analyzer); // 解析查询字符串 var query = parser.Parse(queryStr); // 执行查询 var hits = searcher.Search(query, null, 10, Sort.RELEVANCE).ScoreDocs; // 遍历查询结果 foreach (var hit in hits) { // 获取文档 var doc = searcher.Doc(hit.Doc); // 输出文档内容 Console.WriteLine(doc.Get("content")); } // 关闭索引搜索器 searcher.Dispose(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值