Source Code Cross Referenced for RangeQuery.java in  » Net » lucene-connector » org » apache » lucene » search » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » lucene connector » org.apache.lucene.search 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.search;
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.index.Term;
023:        import org.apache.lucene.index.TermEnum;
024:        import org.apache.lucene.index.IndexReader;
025:        import org.apache.lucene.util.ToStringUtils;
026:
027:        /**
028:         * A Query that matches documents within an exclusive range. A RangeQuery
029:         * is built by QueryParser for input like <code>[010 TO 120]</code> but only if the QueryParser has 
030:         * the useOldRangeQuery property set to true. The QueryParser default behaviour is to use
031:         * the newer ConstantScoreRangeQuery class. This is generally preferable because:
032:         * <ul>
033:         * 	<li>It is faster than RangeQuery</li>
034:         * 	<li>Unlike RangeQuery, it does not cause a BooleanQuery.TooManyClauses exception if the range of values is large</li>
035:         * 	<li>Unlike RangeQuery it does not influence scoring based on the scarcity of individual terms that may match</li>
036:         * </ul>
037:         * 
038:         * 
039:         * @see ConstantScoreRangeQuery
040:         * 
041:         *
042:         * @version $Id: RangeQuery.java 520891 2007-03-21 13:58:47Z yonik $
043:         */
044:        public class RangeQuery extends Query {
045:            private Term lowerTerm;
046:            private Term upperTerm;
047:            private boolean inclusive;
048:
049:            /** Constructs a query selecting all terms greater than
050:             * <code>lowerTerm</code> but less than <code>upperTerm</code>.
051:             * There must be at least one term and either term may be null,
052:             * in which case there is no bound on that side, but if there are
053:             * two terms, both terms <b>must</b> be for the same field.
054:             */
055:            public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive) {
056:                if (lowerTerm == null && upperTerm == null) {
057:                    throw new IllegalArgumentException(
058:                            "At least one term must be non-null");
059:                }
060:                if (lowerTerm != null && upperTerm != null
061:                        && lowerTerm.field() != upperTerm.field()) {
062:                    throw new IllegalArgumentException(
063:                            "Both terms must be for the same field");
064:                }
065:
066:                // if we have a lowerTerm, start there. otherwise, start at beginning
067:                if (lowerTerm != null) {
068:                    this .lowerTerm = lowerTerm;
069:                } else {
070:                    this .lowerTerm = new Term(upperTerm.field(), "");
071:                }
072:
073:                this .upperTerm = upperTerm;
074:                this .inclusive = inclusive;
075:            }
076:
077:            public Query rewrite(IndexReader reader) throws IOException {
078:
079:                BooleanQuery query = new BooleanQuery(true);
080:                TermEnum enumerator = reader.terms(lowerTerm);
081:
082:                try {
083:
084:                    boolean checkLower = false;
085:                    if (!inclusive) // make adjustments to set to exclusive
086:                        checkLower = true;
087:
088:                    String testField = getField();
089:
090:                    do {
091:                        Term term = enumerator.term();
092:                        if (term != null && term.field() == testField) { // interned comparison
093:                            if (!checkLower
094:                                    || term.text().compareTo(lowerTerm.text()) > 0) {
095:                                checkLower = false;
096:                                if (upperTerm != null) {
097:                                    int compare = upperTerm.text().compareTo(
098:                                            term.text());
099:                                    /* if beyond the upper term, or is exclusive and
100:                                     * this is equal to the upper term, break out */
101:                                    if ((compare < 0)
102:                                            || (!inclusive && compare == 0))
103:                                        break;
104:                                }
105:                                TermQuery tq = new TermQuery(term); // found a match
106:                                tq.setBoost(getBoost()); // set the boost
107:                                query.add(tq, BooleanClause.Occur.SHOULD); // add to query
108:                            }
109:                        } else {
110:                            break;
111:                        }
112:                    } while (enumerator.next());
113:                } finally {
114:                    enumerator.close();
115:                }
116:                return query;
117:            }
118:
119:            /** Returns the field name for this query */
120:            public String getField() {
121:                return (lowerTerm != null ? lowerTerm.field() : upperTerm
122:                        .field());
123:            }
124:
125:            /** Returns the lower term of this range query */
126:            public Term getLowerTerm() {
127:                return lowerTerm;
128:            }
129:
130:            /** Returns the upper term of this range query */
131:            public Term getUpperTerm() {
132:                return upperTerm;
133:            }
134:
135:            /** Returns <code>true</code> if the range query is inclusive */
136:            public boolean isInclusive() {
137:                return inclusive;
138:            }
139:
140:            /** Prints a user-readable version of this query. */
141:            public String toString(String field) {
142:                StringBuffer buffer = new StringBuffer();
143:                if (!getField().equals(field)) {
144:                    buffer.append(getField());
145:                    buffer.append(":");
146:                }
147:                buffer.append(inclusive ? "[" : "{");
148:                buffer.append(lowerTerm != null ? lowerTerm.text() : "null");
149:                buffer.append(" TO ");
150:                buffer.append(upperTerm != null ? upperTerm.text() : "null");
151:                buffer.append(inclusive ? "]" : "}");
152:                buffer.append(ToStringUtils.boost(getBoost()));
153:                return buffer.toString();
154:            }
155:
156:            /** Returns true iff <code>o</code> is equal to this. */
157:            public boolean equals(Object o) {
158:                if (this  == o)
159:                    return true;
160:                if (!(o instanceof  RangeQuery))
161:                    return false;
162:
163:                final RangeQuery other = (RangeQuery) o;
164:                if (this .getBoost() != other.getBoost())
165:                    return false;
166:                if (this .inclusive != other.inclusive)
167:                    return false;
168:                // one of lowerTerm and upperTerm can be null
169:                if (this .lowerTerm != null ? !this .lowerTerm
170:                        .equals(other.lowerTerm) : other.lowerTerm != null)
171:                    return false;
172:                if (this .upperTerm != null ? !this .upperTerm
173:                        .equals(other.upperTerm) : other.upperTerm != null)
174:                    return false;
175:                return true;
176:            }
177:
178:            /** Returns a hash code value for this object.*/
179:            public int hashCode() {
180:                int h = Float.floatToIntBits(getBoost());
181:                h ^= lowerTerm != null ? lowerTerm.hashCode() : 0;
182:                // reversible mix to make lower and upper position dependent and
183:                // to prevent them from cancelling out.
184:                h ^= (h << 25) | (h >>> 8);
185:                h ^= upperTerm != null ? upperTerm.hashCode() : 0;
186:                h ^= this .inclusive ? 0x2742E74A : 0;
187:                return h;
188:            }
189:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.