~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/payloads/PayloadHelper.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.search.payloads;
2
 
 
3
 
/**
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
20
 
import java.io.IOException;
21
 
import java.io.Reader;
22
 
import java.util.Random;
23
 
 
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;
39
 
 
40
 
import static org.apache.lucene.util.LuceneTestCase.TEST_VERSION_CURRENT;
41
 
 
42
 
/**
43
 
 *
44
 
 *
45
 
 **/
46
 
public class PayloadHelper {
47
 
 
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";
54
 
 
55
 
  public IndexReader reader;
56
 
 
57
 
  public final class PayloadAnalyzer extends Analyzer {
58
 
 
59
 
 
60
 
    @Override
61
 
    public TokenStream tokenStream(String fieldName, Reader reader) {
62
 
      TokenStream result = new LowerCaseTokenizer(TEST_VERSION_CURRENT, reader);
63
 
      result = new PayloadFilter(result, fieldName);
64
 
      return result;
65
 
    }
66
 
  }
67
 
 
68
 
  public final class PayloadFilter extends TokenFilter {
69
 
    private final String fieldName;
70
 
    private int numSeen = 0;
71
 
    private final PayloadAttribute payloadAtt;
72
 
    
73
 
    public PayloadFilter(TokenStream input, String fieldName) {
74
 
      super(input);
75
 
      this.fieldName = fieldName;
76
 
      payloadAtt = addAttribute(PayloadAttribute.class);
77
 
    }
78
 
 
79
 
    @Override
80
 
    public boolean incrementToken() throws IOException {
81
 
      
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));
88
 
          }
89
 
          else {
90
 
            payloadAtt.setPayload(new Payload(payloadMultiField2));
91
 
          }
92
 
          numSeen++;
93
 
        }
94
 
        return true;
95
 
      }
96
 
      return false;
97
 
    }
98
 
 
99
 
    @Override
100
 
    public void reset() throws IOException {
101
 
      super.reset();
102
 
      this.numSeen = 0;
103
 
    }
104
 
  }
105
 
 
106
 
  /**
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
113
 
   */
114
 
  // TODO: randomize
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);
127
 
    }
128
 
    reader = IndexReader.open(writer, true);
129
 
    writer.close();
130
 
 
131
 
    IndexSearcher searcher = LuceneTestCase.newSearcher(reader);
132
 
    searcher.setSimilarity(similarity);
133
 
    return searcher;
134
 
  }
135
 
 
136
 
  public void tearDown() throws Exception {
137
 
    reader.close();
138
 
  }
139
 
}