1 package org.apache.lucene.benchmark.byTask.feeds;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.util.Properties;
23 import org.apache.lucene.analysis.SimpleAnalyzer;
24 import org.apache.lucene.analysis.WhitespaceAnalyzer;
25 import org.apache.lucene.benchmark.BenchmarkTestCase;
26 import org.apache.lucene.benchmark.byTask.PerfRunData;
27 import org.apache.lucene.benchmark.byTask.tasks.AddDocTask;
28 import org.apache.lucene.benchmark.byTask.tasks.CloseIndexTask;
29 import org.apache.lucene.benchmark.byTask.tasks.CreateIndexTask;
30 import org.apache.lucene.benchmark.byTask.tasks.TaskSequence;
31 import org.apache.lucene.benchmark.byTask.utils.Config;
32 import org.apache.lucene.document.Document;
33 import org.apache.lucene.index.Term;
34 import org.apache.lucene.search.IndexSearcher;
35 import org.apache.lucene.search.TermQuery;
36 import org.apache.lucene.search.TopDocs;
38 /** Tests the functionality of {@link DocMaker}. */
39 public class DocMakerTest extends BenchmarkTestCase {
41 static final class OneDocSource extends ContentSource {
43 private boolean finish = false;
46 public void close() throws IOException {
50 public DocData getNextDocData(DocData docData) throws NoMoreDataException,
53 throw new NoMoreDataException();
56 docData.setBody("body");
57 docData.setDate("date");
58 docData.setTitle("title");
59 Properties props = new Properties();
60 props.setProperty("key", "value");
61 docData.setProps(props);
69 private void doTestIndexProperties(boolean setIndexProps,
70 boolean indexPropsVal, int numExpectedResults) throws Exception {
71 Properties props = new Properties();
73 // Indexing configuration.
74 props.setProperty("analyzer", WhitespaceAnalyzer.class.getName());
75 props.setProperty("content.source", OneDocSource.class.getName());
76 props.setProperty("directory", "RAMDirectory");
78 props.setProperty("doc.index.props", Boolean.toString(indexPropsVal));
82 Config config = new Config(props);
83 PerfRunData runData = new PerfRunData(config);
85 TaskSequence tasks = new TaskSequence(runData, getName(), null, false);
86 tasks.addTask(new CreateIndexTask(runData));
87 tasks.addTask(new AddDocTask(runData));
88 tasks.addTask(new CloseIndexTask(runData));
91 IndexSearcher searcher = new IndexSearcher(runData.getDirectory(), true);
92 TopDocs td = searcher.search(new TermQuery(new Term("key", "value")), 10);
93 assertEquals(numExpectedResults, td.totalHits);
97 private Document createTestNormsDocument(boolean setNormsProp,
98 boolean normsPropVal, boolean setBodyNormsProp, boolean bodyNormsVal)
100 Properties props = new Properties();
102 // Indexing configuration.
103 props.setProperty("analyzer", WhitespaceAnalyzer.class.getName());
104 props.setProperty("content.source", OneDocSource.class.getName());
105 props.setProperty("directory", "RAMDirectory");
107 props.setProperty("doc.tokenized.norms", Boolean.toString(normsPropVal));
109 if (setBodyNormsProp) {
110 props.setProperty("doc.body.tokenized.norms", Boolean.toString(bodyNormsVal));
113 // Create PerfRunData
114 Config config = new Config(props);
116 DocMaker dm = new DocMaker();
117 dm.setConfig(config);
118 return dm.makeDocument();
121 /* Tests doc.index.props property. */
122 public void testIndexProperties() throws Exception {
123 // default is to not index properties.
124 doTestIndexProperties(false, false, 0);
126 // set doc.index.props to false.
127 doTestIndexProperties(true, false, 0);
129 // set doc.index.props to true.
130 doTestIndexProperties(true, true, 1);
133 /* Tests doc.tokenized.norms and doc.body.tokenized.norms properties. */
134 public void testNorms() throws Exception {
138 // Don't set anything, use the defaults
139 doc = createTestNormsDocument(false, false, false, false);
140 assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
141 assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
143 // Set norms to false
144 doc = createTestNormsDocument(true, false, false, false);
145 assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
146 assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
149 doc = createTestNormsDocument(true, true, false, false);
150 assertFalse(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
151 assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
153 // Set body norms to false
154 doc = createTestNormsDocument(false, false, true, false);
155 assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
156 assertTrue(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());
158 // Set body norms to true
159 doc = createTestNormsDocument(false, false, true, true);
160 assertTrue(doc.getField(DocMaker.TITLE_FIELD).getOmitNorms());
161 assertFalse(doc.getField(DocMaker.BODY_FIELD).getOmitNorms());