• 【Lucene4.8教程之四】分析



    1、基础内容

    (1)相关概念

    分析(Analysis),在Lucene中指的是将域(Field)文本转换成最主要的索引表示单元--项(Term)的过程。在搜索过程中,这些项用于决定什么样的文档可以匹配查词条件。

    分析器对分析操作进行了封装,它通过运行若干操作,将文本转化成语汇单元,这个处理过程也称为语汇单元化过程(tokenization)。而从文本洲中提取的文本块称为语汇单元(token)。词汇单元与它的域名结合后,就形成了项。

    (2)何时使用分析器

    • 建立索引期间
    		Directory returnIndexDir = FSDirectory.open(indexDir);
    
    		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48,
    				new StandardAnalyzer(Version.LUCENE_48));
    
    		IndexWriter writer = new IndexWriter(returnIndexDir, iwc);
    • 使用QueryParser对象进行搜索时
    QueryParser parser = new QueryParser(Version.LUCENE_48, "contents",
    				new SimpleAnalyzer(Version.LUCENE_48));
    • 在搜索中高亮显示结果时
    (3)经常使用的4个分析器:
    • WhitespaceAnalyzer, as the name implies, simply splits text into tokens on whitespace characters and makes no other effort to normalize the tokens.
    • SimpleAnalyzer first splits tokens at non-letter characters, then lowercases each token. Be careful! This analyzer quietly discards numeric characters.
    • StopAnalyzer is the same as SimpleAnalyzer, except it removes common words (called stop words, described more in section XXX). By default it removes common words in the English language (the, a, etc.), though you can pass in your own set.
    • StandardAnalyzer is Lucene’s most sophisticated core analyzer. It has quite a bit of logic to identify certain kinds of tokens, such as company names,

    四、其他内容

    在创建IndexWriter时,须要指定分析器,如:
    <span>		</span>IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48,
    <span>				</span>new StandardAnalyzer(Version.LUCENE_48));
    
    <span>		</span>writer = new IndexWriter(returnIndexDir, iwc);
    便在每次向writer中加入文档时。能够针对该文档指定一个分析器,如
    writer.addDocument(doc, new SimpleAnalyzer(Version.LUCENE_48));



  • 相关阅读:
    序列化与反序列化之Kryo
    集合框架
    dubbo配置方式简单介绍
    sql(Oracle)优化之索引
    多级反向代理下,Java获取请求客户端的真实IP地址多中方法整合
    Storm 性能优化
    web.xml配置重理解
    Java各个版本的新特性
    这两天光写shell了,再贴一段代码,以供日后参考。
    Hadoop常用操作笔记
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5365500.html
Copyright © 2020-2023  润新知