01: package org.apache.lucene;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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.util.GregorianCalendar;
21:
22: import org.apache.lucene.store.*;
23: import org.apache.lucene.document.*;
24: import org.apache.lucene.analysis.*;
25: import org.apache.lucene.index.*;
26: import org.apache.lucene.search.*;
27: import org.apache.lucene.queryParser.*;
28:
29: class SearchTest {
30: public static void main(String[] args) {
31: try {
32: Directory directory = new RAMDirectory();
33: Analyzer analyzer = new SimpleAnalyzer();
34: IndexWriter writer = new IndexWriter(directory, analyzer,
35: true);
36:
37: String[] docs = { "a b c d e", "a b c d e a b c d e",
38: "a b c d e f g h i j", "a c e", "e c a",
39: "a c e a c e", "a c e a b c" };
40: for (int j = 0; j < docs.length; j++) {
41: Document d = new Document();
42: d.add(new Field("contents", docs[j], Field.Store.YES,
43: Field.Index.TOKENIZED));
44: writer.addDocument(d);
45: }
46: writer.close();
47:
48: Searcher searcher = new IndexSearcher(directory);
49:
50: String[] queries = {
51: // "a b",
52: // "\"a b\"",
53: // "\"a b c\"",
54: // "a c",
55: // "\"a c\"",
56: "\"a c e\"", };
57: Hits hits = null;
58:
59: QueryParser parser = new QueryParser("contents", analyzer);
60: parser.setPhraseSlop(4);
61: for (int j = 0; j < queries.length; j++) {
62: Query query = parser.parse(queries[j]);
63: System.out.println("Query: "
64: + query.toString("contents"));
65:
66: //DateFilter filter =
67: // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
68: //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
69: //System.out.println(filter);
70:
71: hits = searcher.search(query);
72:
73: System.out.println(hits.length() + " total results");
74: for (int i = 0; i < hits.length() && i < 10; i++) {
75: Document d = hits.doc(i);
76: System.out.println(i + " " + hits.score(i)
77: // + " " + DateField.stringToDate(d.get("modified"))
78: + " " + d.get("contents"));
79: }
80: }
81: searcher.close();
82:
83: } catch (Exception e) {
84: System.out.println(" caught a " + e.getClass()
85: + "\n with message: " + e.getMessage());
86: }
87: }
88:
89: static long Time(int year, int month, int day) {
90: GregorianCalendar calendar = new GregorianCalendar();
91: calendar.set(year, month, day);
92: return calendar.getTime().getTime();
93: }
94: }
|