lucene 3.4 contrib/facet 切面搜尋

來源:互聯網
上載者:User

solr  有facet  search  ,BOBO也有;現在lucene3.4之後也有了,這個是貢獻版本,在apache 官方的包裡面有提供,這種功能對於分組統計和類別統計是一個很好的幫手;

有了這個就不用羨慕solr了,不是我抗拒solr,只是像我們公司有時間讓我們開發的情況下,我更偏向於底層點的api開發,lucene更得心應手。

再說現在的solr沒有近即時搜尋,聽說要4.0後有。

廢話不說,直接上代碼

public class Indexer {

//需要索引的資訊
public static String[] docTitles = {
"white car",
"white dog",
};
public static String[] docTexts = {
"the white car is the one I want.",
"the white dog does not belong to anyone.",
};

//分的類別
public static CategoryPath[] categories = {
new CategoryPath("root","a","f1"), new CategoryPath("root","a","f2")
};

public static void index (Directory indexDir, Directory taxoDir) throws Exception {
// 建立一個普通的indexwriter
IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
//建立一個 taxonomy writer
TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);

int nDocsAdded = 0;
int nFacetsAdded = 0;
for (int docNum=0; docNum<docTexts.length; docNum++)
{

// 準備當前的切面
List<CategoryPath> facetList = SimpleUtils.categoryPathArrayToList(categories[0]);

//
CategoryDocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo).setCategoryPaths(facetList);

// 建立document
Document doc = new Document();
doc.add(new Field(SimpleUtils.TITLE, docTitles[docNum], Store.YES, Index.ANALYZED));
doc.add(new Field(SimpleUtils.TEXT, docTexts[docNum], Store.NO, Index.ANALYZED));

// 把切面的索引資訊添加到document
categoryDocBuilder.build(doc);

// 最終寫入索引
iw.addDocument(doc);

nDocsAdded ++;
nFacetsAdded += facetList.size();
}

// commit changes.
// we commit changes to the taxonomy index prior to committing them to the search index.
// this is important, so that all facets referred to by documents in the search index
// will indeed exist in the taxonomy index.
taxo.commit();
iw.commit();

// close the taxonomy index and the index - all modifications are
// now safely in the provided directories: indexDir and taxoDir.
taxo.close();
iw.close();
System.out.println("Indexed "+nDocsAdded+" documents with overall "+nFacetsAdded+" facets.");
}

public static void main(String[] args){
String indexp = "D:/work/data/index/facet_index/n_index";
String indexp_t = "D:/work/data/index/facet_index/t_index";
try {
Directory directory = FSDirectory.open(new File(indexp));
Directory directory_t = FSDirectory.open(new File(indexp_t));

new Indexer().index(directory, directory_t);
} catch (Exception e) {
e.printStackTrace();
}
}

}

 

 

 

public class Searcher {

public void query(Directory indexDir, Directory taxoDir) throws CorruptIndexException, IOException
{
Query q = new TermQuery(new Term(SimpleUtils.TEXT, "white"));

IndexReader indexReader = IndexReader.open(indexDir, true);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10, true);

FacetIndexingParams indexingParams = new DefaultFacetIndexingParams();
FacetSearchParams facetSearchParams = new FacetSearchParams(indexingParams);
facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("root","a"), 10));

FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxoReader);

// perform documents search and facets accumulation
searcher.search(q, MultiCollector.wrap(topDocsCollector, facetsCollector));

// Obtain facets results and print them
List<FacetResult> res = facetsCollector.getFacetResults();
System.out.println(res.size());
int i = 0;
for (FacetResult facetResult : res) {
System.out.println("Res " + (i++) + ": " + facetResult);
System.out.println("-------------------------------------");
System.out.println( facetResult.getFacetResultNode().getNumSubResults());
System.out.println( facetResult.getFacetResultNode().getOrdinal());
System.out.println( facetResult.getFacetResultNode().getValue());
System.out.println( facetResult.getFacetResultNode().getResidue());
}
}

public static void main(String[] args) {
String indexp = "D:/work/data/index/facet_index/n_index";
String indexp_t = "D:/work/data/index/facet_index/t_index";
try {
Directory directory = FSDirectory.open(new File(indexp));
Directory directory_t = FSDirectory.open(new File(indexp_t));

new Searcher().query(directory, directory_t);
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

在最新的lucene.net 3.0.3中也有facet  search

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.