Source Code Cross Referenced for TestSpansAdvanced.java in  » Net » lucene-connector » org » apache » lucene » search » spans » 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.spans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.search.spans;
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.util.LuceneTestCase;
023:
024:        import org.apache.lucene.analysis.standard.StandardAnalyzer;
025:        import org.apache.lucene.document.Document;
026:        import org.apache.lucene.document.Field;
027:        import org.apache.lucene.index.IndexWriter;
028:        import org.apache.lucene.index.Term;
029:        import org.apache.lucene.search.*;
030:        import org.apache.lucene.store.Directory;
031:        import org.apache.lucene.store.RAMDirectory;
032:
033:        /*******************************************************************************
034:         * Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't
035:         * work correctly in a BooleanQuery.
036:         *
037:         * @author Reece Wilton
038:         */
039:        public class TestSpansAdvanced extends LuceneTestCase {
040:
041:            // location to the index
042:            protected Directory mDirectory;;
043:
044:            protected IndexSearcher searcher;
045:
046:            // field names in the index
047:            private final static String FIELD_ID = "ID";
048:            protected final static String FIELD_TEXT = "TEXT";
049:
050:            /**
051:             * Initializes the tests by adding 4 identical documents to the index.
052:             */
053:            protected void setUp() throws Exception {
054:                super .setUp();
055:                super .setUp();
056:
057:                // create test index
058:                mDirectory = new RAMDirectory();
059:                final IndexWriter writer = new IndexWriter(mDirectory,
060:                        new StandardAnalyzer(), true);
061:                addDocument(writer, "1", "I think it should work.");
062:                addDocument(writer, "2", "I think it should work.");
063:                addDocument(writer, "3", "I think it should work.");
064:                addDocument(writer, "4", "I think it should work.");
065:                writer.close();
066:                searcher = new IndexSearcher(mDirectory);
067:            }
068:
069:            protected void tearDown() throws Exception {
070:                super .tearDown();
071:                searcher.close();
072:                mDirectory.close();
073:                mDirectory = null;
074:            }
075:
076:            /**
077:             * Adds the document to the index.
078:             *
079:             * @param writer the Lucene index writer
080:             * @param id the unique id of the document
081:             * @param text the text of the document
082:             * @throws IOException
083:             */
084:            protected void addDocument(final IndexWriter writer,
085:                    final String id, final String text) throws IOException {
086:
087:                final Document document = new Document();
088:                document.add(new Field(FIELD_ID, id, Field.Store.YES,
089:                        Field.Index.UN_TOKENIZED));
090:                document.add(new Field(FIELD_TEXT, text, Field.Store.YES,
091:                        Field.Index.TOKENIZED));
092:                writer.addDocument(document);
093:            }
094:
095:            /**
096:             * Tests two span queries.
097:             *
098:             * @throws IOException
099:             */
100:            public void testBooleanQueryWithSpanQueries() throws IOException {
101:
102:                doTestBooleanQueryWithSpanQueries(searcher, 0.3884282f);
103:            }
104:
105:            /**
106:             * Tests two span queries.
107:             *
108:             * @throws IOException
109:             */
110:            protected void doTestBooleanQueryWithSpanQueries(IndexSearcher s,
111:                    final float expectedScore) throws IOException {
112:
113:                final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT,
114:                        "work"));
115:                final BooleanQuery query = new BooleanQuery();
116:                query.add(spanQuery, BooleanClause.Occur.MUST);
117:                query.add(spanQuery, BooleanClause.Occur.MUST);
118:                final String[] expectedIds = new String[] { "1", "2", "3", "4" };
119:                final float[] expectedScores = new float[] { expectedScore,
120:                        expectedScore, expectedScore, expectedScore };
121:                assertHits(s, query, "two span queries", expectedIds,
122:                        expectedScores);
123:            }
124:
125:            /**
126:             * Checks to see if the hits are what we expected.
127:             *
128:             * @param query the query to execute
129:             * @param description the description of the search
130:             * @param expectedIds the expected document ids of the hits
131:             * @param expectedScores the expected scores of the hits
132:             *
133:             * @throws IOException
134:             */
135:            protected static void assertHits(Searcher s, Query query,
136:                    final String description, final String[] expectedIds,
137:                    final float[] expectedScores) throws IOException {
138:                QueryUtils.check(query, s);
139:
140:                final float tolerance = 1e-5f;
141:
142:                // Hits hits = searcher.search(query);
143:                // hits normalizes and throws things off if one score is greater than 1.0
144:                TopDocs topdocs = s.search(query, null, 10000);
145:
146:                /*****
147:                // display the hits
148:                System.out.println(hits.length() + " hits for search: \"" + description + '\"');
149:                for (int i = 0; i < hits.length(); i++) {
150:                    System.out.println("  " + FIELD_ID + ':' + hits.doc(i).get(FIELD_ID) + " (score:" + hits.score(i) + ')');
151:                }
152:                 *****/
153:
154:                // did we get the hits we expected
155:                assertEquals(expectedIds.length, topdocs.totalHits);
156:                for (int i = 0; i < topdocs.totalHits; i++) {
157:                    //System.out.println(i + " exp: " + expectedIds[i]);
158:                    //System.out.println(i + " field: " + hits.doc(i).get(FIELD_ID));
159:
160:                    int id = topdocs.scoreDocs[i].doc;
161:                    float score = topdocs.scoreDocs[i].score;
162:                    Document doc = s.doc(id);
163:                    assertEquals(expectedIds[i], doc.get(FIELD_ID));
164:                    boolean scoreEq = Math.abs(expectedScores[i] - score) < tolerance;
165:                    if (!scoreEq) {
166:                        System.out.println(i + " warning, expected score: "
167:                                + expectedScores[i] + ", actual " + score);
168:                        System.out.println(s.explain(query, id));
169:                    }
170:                    assertEquals(expectedScores[i], score, tolerance);
171:                    assertEquals(s.explain(query, id).getValue(), score,
172:                            tolerance);
173:                }
174:            }
175:
176:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.