18.03.01,web学习第六十六天,lucene

66. Lucene+solr

1. Lucene实现全文检索的流程

 

2. 构建文档对象

同一个文档可以有不同的域,不同的文档可以有相同的域

每一个文档有一个id,从0开始加1

3. 分析文档

18.03.01,web学习第六十六天,lucene18.03.01,web学习第六十六天,lucene 

4. 创建索引

 

5. Lucene的使用和入门程序

1)使用包:lucene-core-4.10.3.jar 

 18.03.01,web学习第六十六天,lucene

2)创建document和域对象

 18.03.01,web学习第六十六天,lucene

3)创建indexWriter对象

 18.03.01,web学习第六十六天,lucene

4)创建域

 18.03.01,web学习第六十六天,lucene

 18.03.01,web学习第六十六天,lucene

6. 查询索引

18.03.01,web学习第六十六天,lucene 

 18.03.01,web学习第六十六天,lucene

7. 查看分词效果 了解

//查看标准分析器的分词效果

public void testTokenStream() throws Exception {

//创建一个标准分析器对象

Analyzer analyzer = new StandardAnalyzer();

//获得tokenStream对象

//第一个参数:域名,可以随便给一个

//第二个参数:要分析的文本内容

TokenStream tokenStream = analyzer.tokenStream("test""The Spring Framework provides a comprehensive programming and configuration model.");

//添加一个引用,可以获得每个关键词

CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);

//添加一个偏移量的引用,记录了关键词的开始位置以及结束位置

OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);

//将指针调整到列表的头部

tokenStream.reset();

//遍历关键词列表,通过incrementToken方法判断列表是否结束

while(tokenStream.incrementToken()) {

//关键词的起始位置

System.out.println("start->" + offsetAttribute.startOffset());

//取关键词

System.out.println(charTermAttribute);

//结束位置

System.out.println("end->" + offsetAttribute.endOffset());

}

tokenStream.close();

}

8. 第三方分词器

1)IK Analyzer 2012FF_hf1

 18.03.01,web学习第六十六天,lucene

2)使用方法

   导包,导狠心配置文件

    18.03.01,web学习第六十六天,lucene

在使用分析器的地方 :

IKAnalyzer  analyzer =New IKAnalyzer();

 拓展配置文件:

添加新词,打开配置,添加词:

 18.03.01,web学习第六十六天,lucene

 18.03.01,web学习第六十六天,lucene

3)注意:

搜索时候使用的分析器要和创建索引的时候使用的分析器一致。

9. 索引维护

1)增:

2)删

删除所有:

 18.03.01,web学习第六十六天,lucene

按条件删除:

 18.03.01,web学习第六十六天,lucene

3)修改

 18.03.01,web学习第六十六天,lucene

10. Luncene索引库查询

查询所有

 18.03.01,web学习第六十六天,lucene

 18.03.01,web学习第六十六天,lucene

 18.03.01,web学习第六十六天,lucene

2)TermQuery 精准查询

3)按数值的范围查询 NumericRangeQuery

 18.03.01,web学习第六十六天,lucene

4)组合查询

//组合条件查询

@Test

public void testBooleanQuery() throws Exception {

IndexSearcher indexSearcher = getIndexSearcher();

//创建一个布尔查询对象

BooleanQuery query = new BooleanQuery();

//创建第一个查询条件

Query query1 = new TermQuery(new Term("filename""apache"));

Query query2 = new TermQuery(new Term("content""apache"));

//组合查询条件

query.add(query1, Occur.MUST);

query.add(query2, Occur.MUST);

//执行查询

printResult(query, indexSearcher);

}

Occur.MUST:必须满足此条件,相当于and

Occur.SHOULD:应该满足,但是不满足也可以,相当于or

Occur.MUST_NOT:必须不满足。相当于not

11. 解析查询-条件解释的对象查询

18.03.01,web学习第六十六天,lucene


相关文章: