001: package org.apache.lucene;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021:
022: import org.apache.lucene.store.*;
023: import org.apache.lucene.document.*;
024: import org.apache.lucene.analysis.*;
025: import org.apache.lucene.index.*;
026: import org.apache.lucene.search.*;
027: import org.apache.lucene.queryParser.*;
028:
029: class SearchTestForDuplicates {
030:
031: static final String PRIORITY_FIELD = "priority";
032: static final String ID_FIELD = "id";
033: static final String HIGH_PRIORITY = "high";
034: static final String MED_PRIORITY = "medium";
035: static final String LOW_PRIORITY = "low";
036:
037: public static void main(String[] args) {
038: try {
039: Directory directory = new RAMDirectory();
040: Analyzer analyzer = new SimpleAnalyzer();
041: IndexWriter writer = new IndexWriter(directory, analyzer,
042: true);
043:
044: final int MAX_DOCS = 225;
045:
046: for (int j = 0; j < MAX_DOCS; j++) {
047: Document d = new Document();
048: d.add(new Field(PRIORITY_FIELD, HIGH_PRIORITY,
049: Field.Store.YES, Field.Index.TOKENIZED));
050: d.add(new Field(ID_FIELD, Integer.toString(j),
051: Field.Store.YES, Field.Index.TOKENIZED));
052: writer.addDocument(d);
053: }
054: writer.close();
055:
056: // try a search without OR
057: Searcher searcher = new IndexSearcher(directory);
058: Hits hits = null;
059:
060: QueryParser parser = new QueryParser(PRIORITY_FIELD,
061: analyzer);
062:
063: Query query = parser.parse(HIGH_PRIORITY);
064: System.out.println("Query: "
065: + query.toString(PRIORITY_FIELD));
066:
067: hits = searcher.search(query);
068: printHits(hits);
069:
070: searcher.close();
071:
072: // try a new search with OR
073: searcher = new IndexSearcher(directory);
074: hits = null;
075:
076: parser = new QueryParser(PRIORITY_FIELD, analyzer);
077:
078: query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
079: System.out.println("Query: "
080: + query.toString(PRIORITY_FIELD));
081:
082: hits = searcher.search(query);
083: printHits(hits);
084:
085: searcher.close();
086:
087: } catch (Exception e) {
088: System.out.println(" caught a " + e.getClass()
089: + "\n with message: " + e.getMessage());
090: }
091: }
092:
093: private static void printHits(Hits hits) throws IOException {
094: System.out.println(hits.length() + " total results\n");
095: for (int i = 0; i < hits.length(); i++) {
096: if (i < 10 || (i > 94 && i < 105)) {
097: Document d = hits.doc(i);
098: System.out.println(i + " " + d.get(ID_FIELD));
099: }
100: }
101: }
102:
103: }
|