1 package org.apache.lucene.search.payloads;
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.io.Reader;
22 import java.util.Random;
24 import org.apache.lucene.analysis.*;
25 import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
28 import org.apache.lucene.util.LuceneTestCase;
29 import org.apache.lucene.index.IndexReader;
30 import org.apache.lucene.index.IndexWriter;
31 import org.apache.lucene.index.IndexWriterConfig;
32 import org.apache.lucene.index.Payload;
33 import org.apache.lucene.search.IndexSearcher;
34 import org.apache.lucene.search.Similarity;
35 import org.apache.lucene.store.Directory;
36 import org.apache.lucene.store.MockDirectoryWrapper;
37 import org.apache.lucene.store.RAMDirectory;
38 import org.apache.lucene.util.English;
40 import static org.apache.lucene.util.LuceneTestCase.TEST_VERSION_CURRENT;
46 public class PayloadHelper {
48 private byte[] payloadField = new byte[]{1};
49 private byte[] payloadMultiField1 = new byte[]{2};
50 private byte[] payloadMultiField2 = new byte[]{4};
51 public static final String NO_PAYLOAD_FIELD = "noPayloadField";
52 public static final String MULTI_FIELD = "multiField";
53 public static final String FIELD = "field";
55 public IndexReader reader;
57 public final class PayloadAnalyzer extends Analyzer {
61 public TokenStream tokenStream(String fieldName, Reader reader) {
62 TokenStream result = new LowerCaseTokenizer(TEST_VERSION_CURRENT, reader);
63 result = new PayloadFilter(result, fieldName);
68 public final class PayloadFilter extends TokenFilter {
69 private final String fieldName;
70 private int numSeen = 0;
71 private final PayloadAttribute payloadAtt;
73 public PayloadFilter(TokenStream input, String fieldName) {
75 this.fieldName = fieldName;
76 payloadAtt = addAttribute(PayloadAttribute.class);
80 public boolean incrementToken() throws IOException {
82 if (input.incrementToken()) {
83 if (fieldName.equals(FIELD)) {
84 payloadAtt.setPayload(new Payload(payloadField));
85 } else if (fieldName.equals(MULTI_FIELD)) {
86 if (numSeen % 2 == 0) {
87 payloadAtt.setPayload(new Payload(payloadMultiField1));
90 payloadAtt.setPayload(new Payload(payloadMultiField2));
100 public void reset() throws IOException {
107 * Sets up a RAMDirectory, and adds documents (using English.intToEnglish()) with two fields: field and multiField
108 * and analyzes them using the PayloadAnalyzer
109 * @param similarity The Similarity class to use in the Searcher
110 * @param numDocs The num docs to add
111 * @return An IndexSearcher
112 * @throws IOException
115 public IndexSearcher setUp(Random random, Similarity similarity, int numDocs) throws IOException {
116 Directory directory = new MockDirectoryWrapper(random, new RAMDirectory());
117 PayloadAnalyzer analyzer = new PayloadAnalyzer();
118 IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
119 TEST_VERSION_CURRENT, analyzer).setSimilarity(similarity));
120 // writer.infoStream = System.out;
121 for (int i = 0; i < numDocs; i++) {
122 Document doc = new Document();
123 doc.add(new Field(FIELD, English.intToEnglish(i), Field.Store.YES, Field.Index.ANALYZED));
124 doc.add(new Field(MULTI_FIELD, English.intToEnglish(i) + " " + English.intToEnglish(i), Field.Store.YES, Field.Index.ANALYZED));
125 doc.add(new Field(NO_PAYLOAD_FIELD, English.intToEnglish(i), Field.Store.YES, Field.Index.ANALYZED));
126 writer.addDocument(doc);
128 reader = IndexReader.open(writer, true);
131 IndexSearcher searcher = LuceneTestCase.newSearcher(reader);
132 searcher.setSimilarity(similarity);
136 public void tearDown() throws Exception {