Source Code Cross Referenced for TestTermScorer.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:        import java.util.ArrayList;
022:        import java.util.List;
023:
024:        import org.apache.lucene.util.LuceneTestCase;
025:        import org.apache.lucene.analysis.WhitespaceAnalyzer;
026:        import org.apache.lucene.document.Document;
027:        import org.apache.lucene.document.Field;
028:        import org.apache.lucene.index.IndexReader;
029:        import org.apache.lucene.index.IndexWriter;
030:        import org.apache.lucene.index.Term;
031:        import org.apache.lucene.store.RAMDirectory;
032:
033:        public class TestTermScorer extends LuceneTestCase {
034:            protected RAMDirectory directory;
035:            private static final String FIELD = "field";
036:
037:            protected String[] values = new String[] { "all", "dogs dogs",
038:                    "like", "playing", "fetch", "all" };
039:            protected IndexSearcher indexSearcher;
040:            protected IndexReader indexReader;
041:
042:            public TestTermScorer(String s) {
043:                super (s);
044:            }
045:
046:            protected void setUp() throws Exception {
047:                super .setUp();
048:                directory = new RAMDirectory();
049:
050:                IndexWriter writer = new IndexWriter(directory,
051:                        new WhitespaceAnalyzer(), true);
052:                for (int i = 0; i < values.length; i++) {
053:                    Document doc = new Document();
054:                    doc.add(new Field(FIELD, values[i], Field.Store.YES,
055:                            Field.Index.TOKENIZED));
056:                    writer.addDocument(doc);
057:                }
058:                writer.close();
059:                indexSearcher = new IndexSearcher(directory);
060:                indexReader = indexSearcher.getIndexReader();
061:
062:            }
063:
064:            public void test() throws IOException {
065:
066:                Term allTerm = new Term(FIELD, "all");
067:                TermQuery termQuery = new TermQuery(allTerm);
068:
069:                Weight weight = termQuery.weight(indexSearcher);
070:
071:                TermScorer ts = new TermScorer(weight, indexReader
072:                        .termDocs(allTerm), indexSearcher.getSimilarity(),
073:                        indexReader.norms(FIELD));
074:                assertTrue("ts is null and it shouldn't be", ts != null);
075:                //we have 2 documents with the term all in them, one document for all the other values
076:                final List docs = new ArrayList();
077:                //must call next first
078:
079:                ts.score(new HitCollector() {
080:                    public void collect(int doc, float score) {
081:                        docs.add(new TestHit(doc, score));
082:                        assertTrue("score " + score + " is not greater than 0",
083:                                score > 0);
084:                        assertTrue("Doc: " + doc + " does not equal: " + 0
085:                                + " or doc does not equaal: " + 5, doc == 0
086:                                || doc == 5);
087:                    }
088:                });
089:                assertTrue("docs Size: " + docs.size() + " is not: " + 2, docs
090:                        .size() == 2);
091:                TestHit doc0 = (TestHit) docs.get(0);
092:                TestHit doc5 = (TestHit) docs.get(1);
093:                //The scores should be the same
094:                assertTrue(doc0.score + " does not equal: " + doc5.score,
095:                        doc0.score == doc5.score);
096:                /*
097:                Score should be (based on Default Sim.:
098:                All floats are approximate
099:                tf = 1
100:                numDocs = 6
101:                docFreq(all) = 2
102:                idf = ln(6/3) + 1 = 1.693147
103:                idf ^ 2 = 2.8667
104:                boost = 1
105:                lengthNorm = 1 //there is 1 term in every document
106:                coord = 1
107:                sumOfSquaredWeights = (idf * boost) ^ 2 = 1.693147 ^ 2 = 2.8667
108:                queryNorm = 1 / (sumOfSquaredWeights)^0.5 = 1 /(1.693147) = 0.590
109:
110:                 score = 1 * 2.8667 * 1 * 1 * 0.590 = 1.69
111:
112:                 */
113:                assertTrue(doc0.score + " does not equal: " + 1.6931472f,
114:                        doc0.score == 1.6931472f);
115:            }
116:
117:            public void testNext() throws Exception {
118:
119:                Term allTerm = new Term(FIELD, "all");
120:                TermQuery termQuery = new TermQuery(allTerm);
121:
122:                Weight weight = termQuery.weight(indexSearcher);
123:
124:                TermScorer ts = new TermScorer(weight, indexReader
125:                        .termDocs(allTerm), indexSearcher.getSimilarity(),
126:                        indexReader.norms(FIELD));
127:                assertTrue("ts is null and it shouldn't be", ts != null);
128:                assertTrue("next did not return a doc", ts.next() == true);
129:                assertTrue("score is not correct", ts.score() == 1.6931472f);
130:                assertTrue("next did not return a doc", ts.next() == true);
131:                assertTrue("score is not correct", ts.score() == 1.6931472f);
132:                assertTrue("next returned a doc and it should not have", ts
133:                        .next() == false);
134:            }
135:
136:            public void testSkipTo() throws Exception {
137:
138:                Term allTerm = new Term(FIELD, "all");
139:                TermQuery termQuery = new TermQuery(allTerm);
140:
141:                Weight weight = termQuery.weight(indexSearcher);
142:
143:                TermScorer ts = new TermScorer(weight, indexReader
144:                        .termDocs(allTerm), indexSearcher.getSimilarity(),
145:                        indexReader.norms(FIELD));
146:                assertTrue("ts is null and it shouldn't be", ts != null);
147:                assertTrue("Didn't skip", ts.skipTo(3) == true);
148:                //The next doc should be doc 5
149:                assertTrue("doc should be number 5", ts.doc() == 5);
150:            }
151:
152:            public void testExplain() throws Exception {
153:                Term allTerm = new Term(FIELD, "all");
154:                TermQuery termQuery = new TermQuery(allTerm);
155:
156:                Weight weight = termQuery.weight(indexSearcher);
157:
158:                TermScorer ts = new TermScorer(weight, indexReader
159:                        .termDocs(allTerm), indexSearcher.getSimilarity(),
160:                        indexReader.norms(FIELD));
161:                assertTrue("ts is null and it shouldn't be", ts != null);
162:                Explanation explanation = ts.explain(0);
163:                assertTrue("explanation is null and it shouldn't be",
164:                        explanation != null);
165:                //System.out.println("Explanation: " + explanation.toString());
166:                //All this Explain does is return the term frequency
167:                assertTrue("term frq is not 1", explanation.getValue() == 1);
168:                explanation = ts.explain(1);
169:                assertTrue("explanation is null and it shouldn't be",
170:                        explanation != null);
171:                //System.out.println("Explanation: " + explanation.toString());
172:                //All this Explain does is return the term frequency
173:                assertTrue("term frq is not 0", explanation.getValue() == 0);
174:
175:                Term dogsTerm = new Term(FIELD, "dogs");
176:                termQuery = new TermQuery(dogsTerm);
177:                weight = termQuery.weight(indexSearcher);
178:
179:                ts = new TermScorer(weight, indexReader.termDocs(dogsTerm),
180:                        indexSearcher.getSimilarity(), indexReader.norms(FIELD));
181:                assertTrue("ts is null and it shouldn't be", ts != null);
182:                explanation = ts.explain(1);
183:                assertTrue("explanation is null and it shouldn't be",
184:                        explanation != null);
185:                //System.out.println("Explanation: " + explanation.toString());
186:                //All this Explain does is return the term frequency
187:                float sqrtTwo = (float) Math.sqrt(2.0f);
188:                assertTrue("term frq: " + explanation.getValue()
189:                        + " is not the square root of 2", explanation
190:                        .getValue() == sqrtTwo);
191:
192:                explanation = ts.explain(10);//try a doc out of range
193:                assertTrue("explanation is null and it shouldn't be",
194:                        explanation != null);
195:                //System.out.println("Explanation: " + explanation.toString());
196:                //All this Explain does is return the term frequency
197:
198:                assertTrue("term frq: " + explanation.getValue() + " is not 0",
199:                        explanation.getValue() == 0);
200:
201:            }
202:
203:            private class TestHit {
204:                public int doc;
205:                public float score;
206:
207:                public TestHit(int doc, float score) {
208:                    this .doc = doc;
209:                    this .score = score;
210:                }
211:
212:                public String toString() {
213:                    return "TestHit{" + "doc=" + doc + ", score=" + score + "}";
214:                }
215:            }
216:
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.