pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / test / org / apache / lucene / queryParser / spans / TestSpanQueryParserSimpleSample.java
1 package org.apache.lucene.queryParser.spans;
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 javax.management.Query;
21
22 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
23 import org.apache.lucene.queryParser.core.nodes.OrQueryNode;
24 import org.apache.lucene.queryParser.core.nodes.QueryNode;
25 import org.apache.lucene.queryParser.core.parser.SyntaxParser;
26 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorPipeline;
27 import org.apache.lucene.queryParser.standard.parser.StandardSyntaxParser;
28 import org.apache.lucene.search.spans.SpanQuery;
29 import org.apache.lucene.search.spans.SpanTermQuery;
30 import org.apache.lucene.util.LuceneTestCase;
31
32 /**
33  * This test case demonstrates how the new query parser can be used.<br/>
34  * <br/>
35  * 
36  * It tests queries likes "term", "field:term" "term1 term2" "term1 OR term2",
37  * which are all already supported by the current syntax parser (
38  * {@link StandardSyntaxParser}).<br/>
39  * <br/>
40  * 
41  * The goals is to create a new query parser that supports only the pair
42  * "field:term" or a list of pairs separated or not by an OR operator, and from
43  * this query generate {@link SpanQuery} objects instead of the regular
44  * {@link Query} objects. Basically, every pair will be converted to a
45  * {@link SpanTermQuery} object and if there are more than one pair they will be
46  * grouped by an {@link OrQueryNode}.<br/>
47  * <br/>
48  * 
49  * Another functionality that will be added is the ability to convert every
50  * field defined in the query to an unique specific field.<br/>
51  * <br/>
52  * 
53  * The query generation is divided in three different steps: parsing (syntax),
54  * processing (semantic) and building.<br/>
55  * <br/>
56  * 
57  * The parsing phase, as already mentioned will be performed by the current
58  * query parser: {@link StandardSyntaxParser}.<br/>
59  * <br/>
60  * 
61  * The processing phase will be performed by a processor pipeline which is
62  * compound by 2 processors: {@link SpansValidatorQueryNodeProcessor} and
63  * {@link UniqueFieldQueryNodeProcessor}.
64  * 
65  * <pre>
66  * 
67  *   {@link SpansValidatorQueryNodeProcessor}: as it's going to use the current 
68  *   query parser to parse the syntax, it will support more features than we want,
69  *   this processor basically validates the query node tree generated by the parser
70  *   and just let got through the elements we want, all the other elements as 
71  *   wildcards, range queries, etc...if found, an exception is thrown.
72  *   
73  *   {@link UniqueFieldQueryNodeProcessor}: this processor will take care of reading
74  *   what is the &quot;unique field&quot; from the configuration and convert every field defined
75  *   in every pair to this &quot;unique field&quot;. For that, a {@link SpansQueryConfigHandler} is
76  *   used, which has the {@link UniqueFieldAttribute} defined in it.
77  * </pre>
78  * 
79  * The building phase is performed by the {@link SpansQueryTreeBuilder}, which
80  * basically contains a map that defines which builder will be used to generate
81  * {@link SpanQuery} objects from {@link QueryNode} objects.<br/>
82  * <br/>
83  * 
84  * @see TestSpanQueryParser for a more advanced example
85  * 
86  * @see SpansQueryConfigHandler
87  * @see SpansQueryTreeBuilder
88  * @see SpansValidatorQueryNodeProcessor
89  * @see SpanOrQueryNodeBuilder
90  * @see SpanTermQueryNodeBuilder
91  * @see StandardSyntaxParser
92  * @see UniqueFieldQueryNodeProcessor
93  * @see UniqueFieldAttribute
94  * 
95  */
96 public class TestSpanQueryParserSimpleSample extends LuceneTestCase {
97
98   public void testBasicDemo() throws Exception {
99     SyntaxParser queryParser = new StandardSyntaxParser();
100
101     // convert the CharSequence into a QueryNode tree
102     QueryNode queryTree = queryParser.parse("body:text", null);
103
104     // create a config handler with a attribute used in
105     // UniqueFieldQueryNodeProcessor
106     QueryConfigHandler spanQueryConfigHandler = new SpansQueryConfigHandler();
107     spanQueryConfigHandler.set(SpansQueryConfigHandler.UNIQUE_FIELD, "index");
108
109     // set up the processor pipeline with the ConfigHandler
110     // and create the pipeline for this simple demo
111     QueryNodeProcessorPipeline spanProcessorPipeline = new QueryNodeProcessorPipeline(
112         spanQueryConfigHandler);
113     // @see SpansValidatorQueryNodeProcessor
114     spanProcessorPipeline.add(new SpansValidatorQueryNodeProcessor());
115     // @see UniqueFieldQueryNodeProcessor
116     spanProcessorPipeline.add(new UniqueFieldQueryNodeProcessor());
117
118     // print to show out the QueryNode tree before being processed
119     if (VERBOSE) System.out.println(queryTree);
120
121     // Process the QueryTree using our new Processors
122     queryTree = spanProcessorPipeline.process(queryTree);
123
124     // print to show out the QueryNode tree after being processed
125     if (VERBOSE) System.out.println(queryTree);
126
127     // create a instance off the Builder
128     SpansQueryTreeBuilder spansQueryTreeBuilder = new SpansQueryTreeBuilder();
129
130     // convert QueryNode tree to span query Objects
131     SpanQuery spanquery = spansQueryTreeBuilder.build(queryTree);
132
133     assertTrue(spanquery instanceof SpanTermQuery);
134     assertEquals(spanquery.toString(), "index:text");
135
136   }
137
138 }