01: /*
02: * $Header$
03: * $Revision: 7957 $
04: * $Date: 2007-08-23 04:22:35 -0700 $
05: *
06: * ====================================================================
07: *
08: * Copyright 1999-2004 The Apache Software Foundation
09: *
10: * Licensed under the Apache License, Version 2.0 (the "License");
11: * you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software
17: * distributed under the License is distributed on an "AS IS" BASIS,
18: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: * See the License for the specific language governing permissions and
20: * limitations under the License.
21: *
22: */
23: package org.apache.slide.index.lucene.expressions;
24:
25: import org.apache.lucene.index.Term;
26: import org.apache.lucene.search.BooleanClause;
27: import org.apache.lucene.search.BooleanQuery;
28: import org.apache.lucene.search.TermQuery;
29: import org.apache.lucene.search.WildcardQuery;
30:
31: import org.apache.slide.index.lucene.Index;
32: import org.apache.slide.index.lucene.IndexConfiguration;
33: import org.apache.slide.search.BadQueryException;
34: import org.jdom.Element;
35:
36: /**
37: * Implements the <code>propcontains</code> operator.
38: *
39: * <p>This expression is actually a <code>substring</code> expression.
40: *
41: */
42: public class PropcontainsExpression extends AbstractLuceneExpression {
43:
44: public PropcontainsExpression(Index index, Element element,
45: boolean negated) throws BadQueryException {
46: super (index);
47:
48: Element prop = getPropertyElement(element);
49: String field = IndexConfiguration.generateFieldName(prop
50: .getNamespaceURI(), prop.getName());
51: Element literal = getLiteralElement(element);
52: String text = literal.getTextTrim();
53:
54: if (index.getLogger().isDebugEnabled()) {
55: index.getLogger().debug(
56: "Generating Lucene PropcontainsExpression: prop:"
57: + field + " -> literal:" + text);
58: }
59:
60: if (!index.getConfiguration().isCaseSensitive())
61: text = text.toLowerCase();
62:
63: setQuery(new WildcardQuery(new Term(field, "*" + text + "*")));
64:
65: if (negated) {
66: BooleanQuery booleanQuery = new BooleanQuery();
67: booleanQuery.add(new TermQuery(new Term(
68: Index.IS_DEFINED_FIELD_NAME, field)),
69: BooleanClause.Occur.MUST);
70: booleanQuery.add(getQuery(), BooleanClause.Occur.MUST_NOT); // prohibited
71: setQuery(booleanQuery);
72: }
73: }
74: }
|