001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr;
017:
018: import org.apache.solr.request.*;
019: import org.apache.solr.util.*;
020: import org.w3c.dom.Document;
021:
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.parsers.DocumentBuilder;
024: import java.io.IOException;
025: import java.io.StringWriter;
026: import java.io.ByteArrayInputStream;
027: import java.io.UnsupportedEncodingException;
028: import java.util.Map;
029: import java.util.HashMap;
030: import java.util.regex.Pattern;
031:
032: /**
033: * Tests some basic functionality of the DisMaxRequestHandler
034: */
035: public class DisMaxRequestHandlerTest extends AbstractSolrTestCase {
036:
037: public String getSchemaFile() {
038: return "schema.xml";
039: }
040:
041: public String getSolrConfigFile() {
042: return "solrconfig.xml";
043: }
044:
045: public void setUp() throws Exception {
046: super .setUp();
047: lrf = h.getRequestFactory("dismax", 0, 20, "version", "2.0",
048: "facet", "true", "facet.field", "t_s");
049: }
050:
051: /** Add some documents to the index */
052: protected void populate() {
053: assertU(adoc("id", "666", "features_t", "cool and scary stuff",
054: "subject", "traveling in hell", "t_s", "movie",
055: "title", "The Omen", "weight", "87.9", "iind", "666"));
056: assertU(adoc("id", "42", "features_t", "cool stuff", "subject",
057: "traveling the galaxy", "t_s", "movie", "t_s", "book",
058: "title", "Hitch Hiker's Guide to the Galaxy", "weight",
059: "99.45", "iind", "42"));
060: assertU(adoc("id", "1", "features_t", "nothing", "subject",
061: "garbage", "t_s", "book", "title",
062: "Most Boring Guide Ever", "weight", "77", "iind", "4"));
063: assertU(adoc("id", "8675309", "features_t",
064: "Wikedly memorable chorus and stuff", "subject",
065: "One Cool Hot Chick", "t_s", "song", "title", "Jenny",
066: "weight", "97.3", "iind", "8675309"));
067: assertU(commit());
068: }
069:
070: public void testSomeStuff() throws Exception {
071: populate();
072:
073: assertQ("basic match", req("guide"), "//*[@numFound='2']",
074: "//lst[@name='facet_fields']/lst[@name='t_s']",
075: "*[count(//lst[@name='t_s']/int)=3]",
076: "//lst[@name='t_s']/int[@name='book'][.='2']",
077: "//lst[@name='t_s']/int[@name='movie'][.='1']");
078:
079: assertQ(
080: "basic cross field matching, boost on same field matching",
081: req("cool stuff"), "//*[@numFound='3']",
082: "//result/doc[1]/int[@name='id'][.='42']",
083: "//result/doc[2]/int[@name='id'][.='666']",
084: "//result/doc[3]/int[@name='id'][.='8675309']");
085:
086: assertQ("multi qf", req("q", "cool", "qt", "dismax", "version",
087: "2.0", "qf", "subject", "qf", "features_t"),
088: "//*[@numFound='3']");
089:
090: assertQ("boost query", req("q", "cool stuff", "qt", "dismax",
091: "version", "2.0", "bq", "subject:hell^400"),
092: "//*[@numFound='3']",
093: "//result/doc[1]/int[@name='id'][.='666']",
094: "//result/doc[2]/int[@name='id'][.='42']",
095: "//result/doc[3]/int[@name='id'][.='8675309']");
096:
097: assertQ("multi boost query", req("q", "cool stuff", "qt",
098: "dismax", "version", "2.0", "bq", "subject:hell^400",
099: "bq", "subject:cool^4", "debugQuery", "true"),
100: "//*[@numFound='3']",
101: "//result/doc[1]/int[@name='id'][.='666']",
102: "//result/doc[2]/int[@name='id'][.='8675309']",
103: "//result/doc[3]/int[@name='id'][.='42']");
104:
105: assertQ("minimum mm is three", req("cool stuff traveling"),
106: "//*[@numFound='2']",
107: "//result/doc[1]/int[@name='id'][. ='42']",
108: "//result/doc[2]/int[@name='id'][. ='666']");
109:
110: assertQ("at 4 mm allows one missing ",
111: req("cool stuff traveling jenny"), "//*[@numFound='3']");
112:
113: assertQ("relying on ALTQ from config", req("qt", "dismax",
114: "fq", "id:666", "facet", "false"), "//*[@numFound='1']");
115:
116: assertQ("explicit ALTQ", req("qt", "dismax", "q.alt",
117: "id:blahbalh", "fq", "id:666", "facet", "false"),
118: "//*[@numFound='0']");
119:
120: assertQ("no query slop == no match", req("qt", "dismax", "q",
121: "\"cool chick\""), "//*[@numFound='0']");
122: assertQ("query slop == match", req("qt", "dismax", "qs", "2",
123: "q", "\"cool chick\""), "//*[@numFound='1']");
124: }
125:
126: public void testExtraBlankBQ() throws Exception {
127: populate();
128: // if the boost queries are in their own boolean query, the clauses will be
129: // surrounded by ()'s in the debug output
130: Pattern p = Pattern.compile("subject:hell\\s*subject:cool");
131: Pattern p_bool = Pattern
132: .compile("\\(subject:hell\\s*subject:cool\\)");
133: String resp = h.query(req("q", "cool stuff", "qt", "dismax",
134: "version", "2.0", "bq", "subject:hell OR subject:cool",
135: "debugQuery", "true"));
136: assertTrue(p.matcher(resp).find());
137: assertFalse(p_bool.matcher(resp).find());
138:
139: resp = h.query(req("q", "cool stuff", "qt", "dismax",
140: "version", "2.0", "bq", "subject:hell OR subject:cool",
141: "bq", "", "debugQuery", "true"));
142: assertTrue(p.matcher(resp).find());
143: assertTrue(p_bool.matcher(resp).find());
144:
145: }
146:
147: public void testOldStyleDefaults() throws Exception {
148:
149: lrf = h.getRequestFactory("dismax", 0, 20, "version", "2.0",
150: "facet", "true", "facet.field", "t_s");
151: testSomeStuff();
152: }
153:
154: }
|