facet,英文翻译为方面。Lucene中的facet查询其实就是对事物的方面查询。我们以手机举例。一个手机可以有品牌,型号,运营商等多个facet,不同的facet类型可以组合成不同的手机或者手机的集合。如品牌为小米,运营商为移动构成的就是移动发行的所有小米的手机型号(小米1,小米2,小米3)等。而品牌为小米,型号为小米4构成的手机集合就是小米四的所有运营商发行版(小米4移动版,小米4联通版,小米4电信版等)。我们在对一样事物的搜索时也时常使用这种方式,先确定手机品牌,再逐步对型号,运营商等方面进行限制最终得到想要的结果。下面介绍怎么在Lucene中如何使用facet
1.引入相关依赖
<!--引入方面查询(facet search)的依赖-->
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-facet -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-facet</artifactId>
<version>7.2.1</version>
</dependency>
2.建立普通索引的同时建立TaxonomyIndex(分类索引)
@Test
public void buildIndex() throws Exception{
Directory directory = FSDirectory.open(Paths.get(indexDir));
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(new WhitespaceAnalyzer()));
//使用DirectoryTaxonomyWriter写入进行切面查询所需要的Taxonomy索引
Directory taxioDirectory = FSDirectory.open(Paths.get(taxoDir));
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxioDirectory);
FacetsConfig config = new FacetsConfig();
Document doc = new Document();
doc.add(new TextField("device", "手机", Field.Store.YES));
doc.add(new TextField("name", "米1", Field.Store.