熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java Lucene排重實現group by

2022-06-13   來源: Java核心技術 

  package comloongtaolucenetest;

  import javaioIOException;

  import orgapacheluceneanalysisstandardStandardAnalyzer;

  import orgapachelucenedocumentDocument;

  import orgapachelucenedocumentField;

  import orgapachelucenedocumentFieldIndex;

  import orgapachelucenedocumentFieldStore;

  import orgapacheluceneindexCorruptIndexException;

  import orgapacheluceneindexIndexWriter;

  import orgapacheluceneindexTerm;

  import orgapacheluceneindexIndexWriterMaxFieldLength;

  import orgapachelucenesearchDuplicateFilter;

  import orgapachelucenesearchFilter;

  import orgapachelucenesearchIndexSearcher;

  import orgapachelucenesearchQuery;

  import orgapachelucenesearchScoreDoc;

  import orgapachelucenesearchTermQuery;

  import orgapachelucenesearchTopDocs;

  import orgapachelucenestoreDirectory;

  import orgapachelucenestoreLockObtainFailedException;

  import orgapachelucenestoreRAMDirectory;

  import orgapacheluceneutilVersion;

  public class DuplicateFilterTest {

  public static void main(String[] args) {

  Directory dir = new RAMDirectory();

  Document doc = new Document();

  docadd(new Field(id binbin StoreYES IndexNOT_ANALYZED));

  docadd(new Field(string haha StoreYES IndexNOT_ANALYZED));

  docadd(new Field(time StoreYES IndexNOT_ANALYZED));

  docadd(new Field(duplicate StoreYES IndexNOT_ANALYZED));

  Document doc = new Document();

  docadd(new Field(id yaoyao StoreYES IndexNOT_ANALYZED));

  docadd(new Field(string haha StoreYES IndexNOT_ANALYZED));

  docadd(new Field(time StoreYES IndexNOT_ANALYZED));

  doc

  add(new Field(duplicate StoreYES

  IndexNOT_ANALYZED));

  Document doc = new Document();

  docadd(new Field(id zhangjian StoreYES IndexNOT_ANALYZED));

  docadd(new Field(string haha StoreYES IndexNOT_ANALYZED));

  docadd(new Field(time StoreYES IndexNOT_ANALYZED));

  doc

  add(new Field(duplicate StoreYES

  IndexNOT_ANALYZED));

  Document doc = new Document();

  docadd(new Field(id liweicheng StoreYES IndexNOT_ANALYZED));

  docadd(new Field(string haha StoreYES IndexNOT_ANALYZED));

  docadd(new Field(time StoreYES IndexNOT_ANALYZED));

  doc

  add(new Field(duplicate StoreYES

  IndexNOT_ANALYZED));

  try {

  IndexWriter indexWriter = new IndexWriter(dir

  new StandardAnalyzer(VersionLUCENE_) true

  MaxFieldLengthLIMITED);

  indexWriteraddDocument(doc);

  indexWriteraddDocument(doc);

  indexWriteraddDocument(doc);

  indexWriteraddDocument(doc);

  indexWriterclose();

  Query query = new TermQuery(new Term(string haha));

  Filter filter = new DuplicateFilter(duplicate);

  IndexSearcher indexSearcher = new IndexSearcher(dir);

  TopDocs top = indexSearchersearch(query filter );

  ScoreDoc[] scoreDocs = topscoreDocs;

  for (ScoreDoc scoreDoc : scoreDocs) {

  Document rdoc = indexSearcherdoc(scoreDocdoc);

  Systemoutprint(id:+rdocget(id) +  排重ID: +rdocget(duplicate));

  Query queryDuplicate = new TermQuery(new Term(duplicate rdocget(duplicate)));

  Systemoutprintln(轉載:+ indexSearchersearch(queryDuplicate )totalHits

  );

  }

  } catch (CorruptIndexException e) {

  eprintStackTrace();

  } catch (LockObtainFailedException e) {

  eprintStackTrace();

  } catch (IOException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25714.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.