Search in sources :

Example 11 with MergePolicy

use of org.apache.lucene.index.MergePolicy in project lucene-solr by apache.

the class TestSearchForDuplicates method doTest.

private void doTest(Random random, PrintWriter out, boolean useCompoundFiles, int MAX_DOCS) throws Exception {
    Directory directory = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random);
    IndexWriterConfig conf = newIndexWriterConfig(analyzer);
    final MergePolicy mp = conf.getMergePolicy();
    mp.setNoCFSRatio(useCompoundFiles ? 1.0 : 0.0);
    IndexWriter writer = new IndexWriter(directory, conf);
    if (VERBOSE) {
        System.out.println("TEST: now build index MAX_DOCS=" + MAX_DOCS);
    }
    for (int j = 0; j < MAX_DOCS; j++) {
        Document d = new Document();
        d.add(newTextField(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES));
        d.add(new StoredField(ID_FIELD, j));
        d.add(new NumericDocValuesField(ID_FIELD, j));
        writer.addDocument(d);
    }
    writer.close();
    // try a search without OR
    IndexReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = newSearcher(reader);
    Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY));
    out.println("Query: " + query.toString(PRIORITY_FIELD));
    if (VERBOSE) {
        System.out.println("TEST: search query=" + query);
    }
    final Sort sort = new Sort(SortField.FIELD_SCORE, new SortField(ID_FIELD, SortField.Type.INT));
    ScoreDoc[] hits = searcher.search(query, MAX_DOCS, sort).scoreDocs;
    printHits(out, hits, searcher);
    checkHits(hits, MAX_DOCS, searcher);
    // try a new search with OR
    searcher = newSearcher(reader);
    hits = null;
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    booleanQuery.add(new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur.SHOULD);
    booleanQuery.add(new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur.SHOULD);
    out.println("Query: " + booleanQuery.build().toString(PRIORITY_FIELD));
    hits = searcher.search(booleanQuery.build(), MAX_DOCS, sort).scoreDocs;
    printHits(out, hits, searcher);
    checkHits(hits, MAX_DOCS, searcher);
    reader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) StoredField(org.apache.lucene.document.StoredField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) MergePolicy(org.apache.lucene.index.MergePolicy) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

MergePolicy (org.apache.lucene.index.MergePolicy)11 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)5 TieredMergePolicy (org.apache.lucene.index.TieredMergePolicy)5 IndexWriter (org.apache.lucene.index.IndexWriter)3 Sort (org.apache.lucene.search.Sort)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 ConcurrentMergeScheduler (org.apache.lucene.index.ConcurrentMergeScheduler)2 LogMergePolicy (org.apache.lucene.index.LogMergePolicy)2 MergeScheduler (org.apache.lucene.index.MergeScheduler)2 NoMergePolicy (org.apache.lucene.index.NoMergePolicy)2 UpgradeIndexMergePolicy (org.apache.lucene.index.UpgradeIndexMergePolicy)2 SortField (org.apache.lucene.search.SortField)2 Directory (org.apache.lucene.store.Directory)2 SortingMergePolicy (org.apache.solr.index.SortingMergePolicy)2 IndexSchema (org.apache.solr.schema.IndexSchema)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 Document (org.apache.lucene.document.Document)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1