You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by sh...@apache.org on 2009/04/22 10:53:29 UTC
svn commit: r767412 [2/2] - in /lucene/solr/trunk: ./ example/solr/conf/
src/common/org/apache/solr/common/params/ src/java/org/apache/solr/handler/
src/solrj/org/apache/solr/client/solrj/request/
src/solrj/org/apache/solr/client/solrj/response/ src/te...
Added: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/FieldAnalysisResponse.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/FieldAnalysisResponse.java?rev=767412&view=auto
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/FieldAnalysisResponse.java (added)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/FieldAnalysisResponse.java Wed Apr 22 08:53:28 2009
@@ -0,0 +1,202 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.client.solrj.response;
+
+import org.apache.solr.common.util.NamedList;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A response that is returned by processing the {@link org.apache.solr.client.solrj.request.FieldAnalysisRequest}.
+ * Holds a map of {@link Analysis} objects per field name as well as a map of {@link Analysis} objects per field type.
+ *
+ * @version $Id$
+ * @since solr 1.4
+ */
+public class FieldAnalysisResponse extends AnalysisResponseBase {
+
+ private Map<String, Analysis> analysisByFieldTypeName = new HashMap<String, Analysis>();
+ private Map<String, Analysis> analysisByFieldName = new HashMap<String, Analysis>();
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setResponse(NamedList<Object> response) {
+ super.setResponse(response);
+
+ NamedList analysisNL = (NamedList) response.get("analysis");
+
+ NamedList<Object> fieldTypesNL = (NamedList<Object>) analysisNL.get("field_types");
+ for (Map.Entry<String, Object> entry : fieldTypesNL) {
+ Analysis analysis = new Analysis();
+ NamedList fieldTypeNL = (NamedList) entry.getValue();
+ NamedList<Object> queryNL = (NamedList<Object>) fieldTypeNL.get("query");
+ List<AnalysisPhase> phases = (queryNL == null) ? null : buildPhases(queryNL);
+ analysis.setQueryPhases(phases);
+ NamedList<Object> indexNL = (NamedList<Object>) fieldTypeNL.get("index");
+ phases = buildPhases(indexNL);
+ analysis.setIndexPhases(phases);
+ String fieldTypeName = entry.getKey();
+ analysisByFieldTypeName.put(fieldTypeName, analysis);
+ }
+
+ NamedList<Object> fieldNamesNL = (NamedList<Object>) analysisNL.get("field_names");
+ for (Map.Entry<String, Object> entry : fieldNamesNL) {
+ Analysis analysis = new Analysis();
+ NamedList fieldNameNL = (NamedList) entry.getValue();
+ NamedList<Object> queryNL = (NamedList<Object>) fieldNameNL.get("query");
+ List<AnalysisPhase> phases = (queryNL == null) ? null : buildPhases(queryNL);
+ analysis.setQueryPhases(phases);
+ NamedList<Object> indexNL = (NamedList<Object>) fieldNameNL.get("index");
+ phases = buildPhases(indexNL);
+ analysis.setIndexPhases(phases);
+ String fieldName = entry.getKey();
+ analysisByFieldName.put(fieldName, analysis);
+ }
+ }
+
+ /**
+ * Returns the number of field type analyses.
+ *
+ * @return The number of field type analyses.
+ */
+ public int getFieldTypeAnalysisCount() {
+ return analysisByFieldTypeName.size();
+ }
+
+ /**
+ * Returns the analysis for the given field type or {@code null} if no such analysis exists.
+ *
+ * @param fieldTypeName The name of the field type.
+ *
+ * @return The analysis for the given field type.
+ */
+ public Analysis getFieldTypeAnalysis(String fieldTypeName) {
+ return analysisByFieldTypeName.get(fieldTypeName);
+ }
+
+ /**
+ * Returns all field type analyses with their associated field types.
+ *
+ * @return All field type analyses with their associated field types.
+ */
+ public Iterable<Map.Entry<String, Analysis>> getAllFieldTypeAnalysis() {
+ return analysisByFieldTypeName.entrySet();
+ }
+
+ /**
+ * Returns the number of field name analyses.
+ *
+ * @return The number of field name analyses.
+ */
+ public int getFieldNameAnalysisCount() {
+ return analysisByFieldName.size();
+ }
+
+ /**
+ * Returns the analysis for the given field name or {@code null} if no such analysis exists.
+ *
+ * @param fieldName The field name.
+ *
+ * @return The analysis for the given field name.
+ */
+ public Analysis getFieldNameAnalysis(String fieldName) {
+ return analysisByFieldName.get(fieldName);
+ }
+
+ /**
+ * Returns all field name analysese with their associated field names.
+ *
+ * @return all field name analysese with their associated field names.
+ */
+ public Iterable<Map.Entry<String, Analysis>> getAllFieldNameAnalysis() {
+ return analysisByFieldName.entrySet();
+ }
+
+
+ //================================================= Inner Classes ==================================================
+
+ /**
+ * The analysis of a field. Holds a list of all the query time analysis phases (if a query analysis was requested)
+ * as well as index time phases.
+ */
+ public static class Analysis {
+
+ private List<AnalysisPhase> queryPhases;
+ private List<AnalysisPhase> indexPhases;
+
+ /**
+ * This class should only be instantiated internally.
+ */
+ private Analysis() {
+ }
+
+ /**
+ * Returns the number of query time analysis phases in this analysis or {@code -1) if query time analysis
+ * doesn't exist.
+ *
+ * @return Returns the number of query time analysis phases in this analysis or {@code -1) if query time
+ * analysis doesn't exist.
+ */
+ public int getQueryPhasesCount() {
+ return queryPhases == null ? -1 : queryPhases.size();
+ }
+
+ /**
+ * Returns the query time analysis phases for this analysis or {@code null} if query time analysis doesn't
+ * exist.
+ *
+ * @return The query time analysis phases for this analysis or {@code null} if query time analysis doesn't
+ * exist.
+ */
+ public Iterable<AnalysisPhase> getQueryPhases() {
+ return queryPhases;
+ }
+
+ /**
+ * Returns the index time analysis phases for this analysis.
+ *
+ * @return The index time analysis phases for this analysis.
+ */
+ public int getIndexPhasesCount() {
+ return indexPhases.size();
+ }
+
+ /**
+ * Returns the index time analysis phases for this analysis.
+ *
+ * @return The index time analysis phases for this analysis.
+ */
+ public Iterable<AnalysisPhase> getIndexPhases() {
+ return indexPhases;
+ }
+
+ private void setQueryPhases(List<AnalysisPhase> queryPhases) {
+ this.queryPhases = queryPhases;
+ }
+
+ private void setIndexPhases(List<AnalysisPhase> indexPhases) {
+ this.indexPhases = indexPhases;
+ }
+
+ }
+
+}
Propchange: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/FieldAnalysisResponse.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/FieldAnalysisResponse.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/AnlysisResponseBaseTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/AnlysisResponseBaseTest.java?rev=767412&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/AnlysisResponseBaseTest.java (added)
+++ lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/AnlysisResponseBaseTest.java Wed Apr 22 08:53:28 2009
@@ -0,0 +1,121 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.client.solrj.response;
+
+import org.apache.solr.common.util.NamedList;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A Test case for the {@link AnalysisResponseBase} class.
+ *
+ * @version $Id$
+ * @since solr 1.4
+ */
+@SuppressWarnings("unchecked")
+public class AnlysisResponseBaseTest {
+
+ /**
+ * Tests the {@link AnalysisResponseBase#buildTokenInfo(org.apache.solr.common.util.NamedList)} method.
+ */
+ @Test
+ public void testBuildTokenInfo() throws Exception {
+
+ NamedList tokenNL = new NamedList();
+ tokenNL.add("text", "JUMPING");
+ tokenNL.add("type", "word");
+ tokenNL.add("start", 0);
+ tokenNL.add("end", 7);
+ tokenNL.add("position", 1);
+
+ AnalysisResponseBase response = new AnalysisResponseBase();
+
+ AnalysisResponseBase.TokenInfo tokenInfo = response.buildTokenInfo(tokenNL);
+ assertEquals("JUMPING", tokenInfo.getText());
+ assertEquals(null, tokenInfo.getRawText());
+ assertEquals("word", tokenInfo.getType());
+ assertEquals(0, tokenInfo.getStart());
+ assertEquals(7, tokenInfo.getEnd());
+ assertEquals(1, tokenInfo.getPosition());
+ assertFalse(tokenInfo.isMatch());
+
+ tokenNL.add("rawText", "JUMPING1");
+ tokenNL.add("match", true);
+
+ tokenInfo = response.buildTokenInfo(tokenNL);
+ assertEquals("JUMPING", tokenInfo.getText());
+ assertEquals("JUMPING1", tokenInfo.getRawText());
+ assertEquals("word", tokenInfo.getType());
+ assertEquals(0, tokenInfo.getStart());
+ assertEquals(7, tokenInfo.getEnd());
+ assertEquals(1, tokenInfo.getPosition());
+ assertTrue(tokenInfo.isMatch());
+ }
+
+ /**
+ * Tests the {@link AnalysisResponseBase#buildPhases(org.apache.solr.common.util.NamedList)} )} method.
+ */
+ @Test
+ public void testBuildPhases() throws Exception {
+
+ final AnalysisResponseBase.TokenInfo tokenInfo = new AnalysisResponseBase.TokenInfo("text", null, "type", 0, 3, 1, false);
+ NamedList nl = new NamedList();
+ nl.add("Tokenizer", buildFakeTokenInfoList(6));
+ nl.add("Filter1", buildFakeTokenInfoList(5));
+ nl.add("Filter2", buildFakeTokenInfoList(4));
+ nl.add("Filter3", buildFakeTokenInfoList(3));
+
+ AnalysisResponseBase response = new AnalysisResponseBase() {
+ @Override
+ protected TokenInfo buildTokenInfo(NamedList tokenNL) {
+ return tokenInfo;
+ }
+ };
+
+ List<AnalysisResponseBase.AnalysisPhase> phases = response.buildPhases(nl);
+
+ assertEquals(4, phases.size());
+ assertPhase(phases.get(0), "Tokenizer", 6, tokenInfo);
+ assertPhase(phases.get(1), "Filter1", 5, tokenInfo);
+ assertPhase(phases.get(2), "Filter2", 4, tokenInfo);
+ assertPhase(phases.get(3), "Filter3", 3, tokenInfo);
+ }
+
+ //================================================ Helper Methods ==================================================
+
+ private List<NamedList> buildFakeTokenInfoList(int numberOfTokens) {
+ List<NamedList> list = new ArrayList<NamedList>(numberOfTokens);
+ for (int i = 0; i < numberOfTokens; i++) {
+ list.add(new NamedList());
+ }
+ return list;
+ }
+
+ private void assertPhase(AnalysisResponseBase.AnalysisPhase phase, String expectedClassName, int expectedTokenCount, AnalysisResponseBase.TokenInfo expectedToken) {
+
+ assertEquals(expectedClassName, phase.getClassName());
+ List<AnalysisResponseBase.TokenInfo> tokens = phase.getTokens();
+ assertEquals(expectedTokenCount, tokens.size());
+ for (AnalysisResponseBase.TokenInfo token : tokens) {
+ assertSame(expectedToken, token);
+ }
+ }
+}
Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/AnlysisResponseBaseTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/AnlysisResponseBaseTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/DocumentAnalysisResponseTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/DocumentAnalysisResponseTest.java?rev=767412&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/DocumentAnalysisResponseTest.java (added)
+++ lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/DocumentAnalysisResponseTest.java Wed Apr 22 08:53:28 2009
@@ -0,0 +1,153 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.client.solrj.response;
+
+import org.apache.solr.common.util.NamedList;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A test for the {@link DocumentAnalysisResponse} class.
+ *
+ * @version $Id$
+ * @since solr 1.4
+ */
+public class DocumentAnalysisResponseTest {
+
+ /**
+ * Tests the {@link DocumentAnalysisResponse#setResponse(org.apache.solr.common.util.NamedList)} method
+ */
+ @Test
+ public void testSetResponse() throws Exception {
+
+ // the parsing of the analysis phases is already tested in the AnalysisResponseBaseTest. So we can just fake
+ // the phases list here and use it.
+ final List<AnalysisResponseBase.AnalysisPhase> phases = new ArrayList<AnalysisResponseBase.AnalysisPhase>(1);
+ AnalysisResponseBase.AnalysisPhase expectedPhase = new AnalysisResponseBase.AnalysisPhase("Tokenizer");
+ phases.add(expectedPhase);
+
+ NamedList responseNL = buildResponse();
+ DocumentAnalysisResponse response = new DocumentAnalysisResponse() {
+
+ @Override
+ protected List<AnalysisPhase> buildPhases(NamedList<Object> phaseNL) {
+ return phases;
+ }
+ };
+
+ response.setResponse(responseNL);
+
+ assertEquals(1, response.getDocumentAnalysesCount());
+
+ DocumentAnalysisResponse.DocumentAnalysis documentAnalysis = response.getDocumentAnalysis("1");
+ assertEquals("1", documentAnalysis.getDocumentKey());
+ assertEquals(3, documentAnalysis.getFieldAnalysesCount());
+
+ DocumentAnalysisResponse.FieldAnalysis fieldAnalysis = documentAnalysis.getFieldAnalysis("id");
+ assertEquals("id", fieldAnalysis.getFieldName());
+ assertEquals(1, fieldAnalysis.getQueryPhasesCount());
+ AnalysisResponseBase.AnalysisPhase phase = fieldAnalysis.getQueryPhases().iterator().next();
+ assertSame(expectedPhase, phase);
+ assertEquals(1, fieldAnalysis.getValueCount());
+ assertEquals(1, fieldAnalysis.getIndexPhasesCount("1"));
+ phase = fieldAnalysis.getIndexPhases("1").iterator().next();
+ assertSame(expectedPhase, phase);
+
+ fieldAnalysis = documentAnalysis.getFieldAnalysis("name");
+ assertEquals("name", fieldAnalysis.getFieldName());
+ assertEquals(1, fieldAnalysis.getQueryPhasesCount());
+ phase = fieldAnalysis.getQueryPhases().iterator().next();
+ assertSame(expectedPhase, phase);
+ assertEquals(2, fieldAnalysis.getValueCount());
+ assertEquals(1, fieldAnalysis.getIndexPhasesCount("name value 1"));
+ phase = fieldAnalysis.getIndexPhases("name value 1").iterator().next();
+ assertSame(expectedPhase, phase);
+ assertEquals(1, fieldAnalysis.getIndexPhasesCount("name value 2"));
+ phase = fieldAnalysis.getIndexPhases("name value 2").iterator().next();
+ assertSame(expectedPhase, phase);
+
+ fieldAnalysis = documentAnalysis.getFieldAnalysis("text");
+ assertEquals("text", fieldAnalysis.getFieldName());
+ assertEquals(1, fieldAnalysis.getQueryPhasesCount());
+ phase = fieldAnalysis.getQueryPhases().iterator().next();
+ assertSame(expectedPhase, phase);
+ assertEquals(1, fieldAnalysis.getValueCount());
+ assertEquals(1, fieldAnalysis.getIndexPhasesCount("text value"));
+ phase = fieldAnalysis.getIndexPhases("text value").iterator().next();
+ assertSame(expectedPhase, phase);
+ }
+
+ //================================================ Helper Methods ==================================================
+
+ private NamedList buildResponse() {
+
+ NamedList response = new NamedList();
+
+ NamedList responseHeader = new NamedList();
+ response.add("responseHeader", responseHeader);
+
+ NamedList params = new NamedList();
+ responseHeader.add("params", params);
+ params.add("analysis.showmatch", "true");
+ params.add("analysis.query", "the query");
+
+ responseHeader.add("status", 0);
+ responseHeader.add("QTime", 105);
+
+ NamedList analysis = new NamedList();
+ response.add("analysis", analysis);
+
+ NamedList doc1 = new NamedList();
+
+ analysis.add("1", doc1);
+ NamedList id = new NamedList();
+ doc1.add("id", id);
+ NamedList query = new NamedList();
+ id.add("query", query);
+ NamedList index = new NamedList();
+ id.add("index", index);
+ NamedList idValue = new NamedList();
+ index.add("1", idValue);
+
+ NamedList name = new NamedList();
+ doc1.add("name", name);
+ query = new NamedList();
+ name.add("query", query);
+ index = new NamedList();
+ name.add("index", index);
+ NamedList nameValue1 = new NamedList();
+ index.add("name value 1", nameValue1);
+ NamedList nameValue2 = new NamedList();
+ index.add("name value 2", nameValue2);
+
+ NamedList text = new NamedList();
+ doc1.add("text", text);
+ query = new NamedList();
+ text.add("query", query);
+ index = new NamedList();
+ text.add("index", index);
+ NamedList textValue = new NamedList();
+ index.add("text value", textValue);
+
+ return response;
+ }
+}
Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/DocumentAnalysisResponseTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/DocumentAnalysisResponseTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/FieldAnalysisResponseTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/FieldAnalysisResponseTest.java?rev=767412&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/FieldAnalysisResponseTest.java (added)
+++ lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/FieldAnalysisResponseTest.java Wed Apr 22 08:53:28 2009
@@ -0,0 +1,123 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.client.solrj.response;
+
+import org.apache.solr.common.util.NamedList;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * A test case for the {@link FieldAnalysisResponse} class.
+ *
+ * @version $Id$
+ * @since solr 1.4
+ */
+@SuppressWarnings("unchecked")
+public class FieldAnalysisResponseTest {
+
+ /**
+ * Tests the {@link FieldAnalysisResponse#setResponse(org.apache.solr.common.util.NamedList)} method.
+ */
+ @Test
+ public void testSetResponse() throws Exception {
+
+ // the parsing of the analysis phases is already tested in the AnalysisResponseBaseTest. So we can just fake
+ // the phases list here and use it.
+ final List<AnalysisResponseBase.AnalysisPhase> phases = new ArrayList<AnalysisResponseBase.AnalysisPhase>(1);
+ AnalysisResponseBase.AnalysisPhase expectedPhase = new AnalysisResponseBase.AnalysisPhase("Tokenizer");
+ phases.add(expectedPhase);
+
+ NamedList responseNL = buildResponse();
+ FieldAnalysisResponse response = new FieldAnalysisResponse() {
+ @Override
+ protected List<AnalysisPhase> buildPhases(NamedList<Object> phaseNL) {
+ return phases;
+ }
+ };
+
+ response.setResponse(responseNL);
+
+ assertEquals(1, response.getFieldNameAnalysisCount());
+ FieldAnalysisResponse.Analysis analysis = response.getFieldNameAnalysis("name");
+ Iterator<AnalysisResponseBase.AnalysisPhase> iter = analysis.getIndexPhases().iterator();
+ assertTrue(iter.hasNext());
+ assertSame(expectedPhase, iter.next());
+ assertFalse(iter.hasNext());
+ iter = analysis.getQueryPhases().iterator();
+ assertTrue(iter.hasNext());
+ assertSame(expectedPhase, iter.next());
+ assertFalse(iter.hasNext());
+
+ analysis = response.getFieldTypeAnalysis("text");
+ iter = analysis.getIndexPhases().iterator();
+ assertTrue(iter.hasNext());
+ assertSame(expectedPhase, iter.next());
+ assertFalse(iter.hasNext());
+ iter = analysis.getQueryPhases().iterator();
+ assertTrue(iter.hasNext());
+ assertSame(expectedPhase, iter.next());
+ assertFalse(iter.hasNext());
+ }
+
+ //================================================ Helper Methods ==================================================
+
+ private NamedList buildResponse() {
+ NamedList response = new NamedList();
+
+ NamedList responseHeader = new NamedList();
+ response.add("responseHeader", responseHeader);
+
+ NamedList params = new NamedList();
+ responseHeader.add("params", params);
+ params.add("analysis.showmatch", "true");
+ params.add("analysis.query", "the query");
+ params.add("analysis.fieldname", "name");
+ params.add("analysis.fieldvalue", "The field value");
+ params.add("analysis.fieldtype", "text");
+
+ responseHeader.add("status", 0);
+ responseHeader.add("QTime", 66);
+
+ NamedList analysis = new NamedList();
+ response.add("analysis", analysis);
+
+ NamedList fieldTypes = new NamedList();
+ analysis.add("field_types", fieldTypes);
+ NamedList text = new NamedList();
+ fieldTypes.add("text", text);
+ NamedList index = new NamedList();
+ text.add("index", index);
+ NamedList query = new NamedList();
+ text.add("query", query);
+
+ NamedList fieldNames = new NamedList();
+ analysis.add("field_names", fieldNames);
+ NamedList name = new NamedList();
+ fieldNames.add("name", name);
+ index = new NamedList();
+ name.add("index", index);
+ query = new NamedList();
+ name.add("query", query);
+
+ return response;
+ }
+}
Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/FieldAnalysisResponseTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/FieldAnalysisResponseTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: lucene/solr/trunk/src/test/org/apache/solr/handler/AnalysisRequestHandlerTestBase.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/handler/AnalysisRequestHandlerTestBase.java?rev=767412&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/handler/AnalysisRequestHandlerTestBase.java (added)
+++ lucene/solr/trunk/src/test/org/apache/solr/handler/AnalysisRequestHandlerTestBase.java Wed Apr 22 08:53:28 2009
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.handler;
+
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.util.AbstractSolrTestCase;
+
+/**
+ * A base class for all analysis request handler tests.
+ *
+ * @version $Id$
+ * @since solr 1.4
+ */
+public abstract class AnalysisRequestHandlerTestBase extends AbstractSolrTestCase {
+
+ protected void assertToken(NamedList token, TokenInfo info) {
+ assertEquals(info.getText(), token.get("text"));
+ if (info.getRawText() != null) {
+ assertEquals(info.getRawText(), token.get("raw_text"));
+ }
+ assertEquals(info.getType(), token.get("type"));
+ assertEquals(new Integer(info.getStart()), token.get("start"));
+ assertEquals(new Integer(info.getEnd()), token.get("end"));
+ assertEquals(new Integer(info.getPosition()), token.get("position"));
+ if (info.isMatch()) {
+ assertEquals(Boolean.TRUE, token.get("match"));
+ }
+ if (info.getPayload() != null) {
+ assertEquals(info.getPayload(), token.get("payload"));
+ }
+ }
+
+
+ //================================================= Inner Classes ==================================================
+
+ protected class TokenInfo {
+
+ private String text;
+ private String rawText;
+ private String type;
+ private int start;
+ private int end;
+ private String payload;
+ private int position;
+ private boolean match;
+
+ public TokenInfo(
+ String text,
+ String rawText,
+ String type,
+ int start,
+ int end,
+ int position,
+ String payload,
+ boolean match) {
+
+ this.text = text;
+ this.rawText = rawText;
+ this.type = type;
+ this.start = start;
+ this.end = end;
+ this.position = position;
+ this.payload = payload;
+ this.match = match;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public String getRawText() {
+ return rawText;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public int getStart() {
+ return start;
+ }
+
+ public int getEnd() {
+ return end;
+ }
+
+ public String getPayload() {
+ return payload;
+ }
+
+ public int getPosition() {
+ return position;
+ }
+
+ public boolean isMatch() {
+ return match;
+ }
+ }
+
+}
Propchange: lucene/solr/trunk/src/test/org/apache/solr/handler/AnalysisRequestHandlerTestBase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/solr/trunk/src/test/org/apache/solr/handler/AnalysisRequestHandlerTestBase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: lucene/solr/trunk/src/test/org/apache/solr/handler/DocumentAnalysisRequestHandlerTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/handler/DocumentAnalysisRequestHandlerTest.java?rev=767412&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/handler/DocumentAnalysisRequestHandlerTest.java (added)
+++ lucene/solr/trunk/src/test/org/apache/solr/handler/DocumentAnalysisRequestHandlerTest.java Wed Apr 22 08:53:28 2009
@@ -0,0 +1,233 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.handler;
+
+import org.apache.solr.client.solrj.request.DocumentAnalysisRequest;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.SolrInputField;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.common.util.ContentStreamBase;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.request.SolrQueryRequestBase;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A test for {@link DocumentAnalysisRequestHandler}.
+ *
+ * @version $Id$
+ * @since solr 1.4
+ */
+public class DocumentAnalysisRequestHandlerTest extends AnalysisRequestHandlerTestBase {
+
+ private DocumentAnalysisRequestHandler handler;
+
+ @Override
+ public String getSchemaFile() {
+ return "schema.xml";
+ }
+
+ @Override
+ public String getSolrConfigFile() {
+ return "solrconfig.xml";
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ handler = new DocumentAnalysisRequestHandler();
+ handler.init(new NamedList());
+ }
+
+ /**
+ * Tests the {@link DocumentAnalysisRequestHandler#resolveAnalysisRequest(org.apache.solr.request.SolrQueryRequest)}
+ */
+ public void testResolveAnalysisRequest() throws Exception {
+
+ String docsInput =
+ "<docs>" +
+ "<doc>" +
+ "<field name=\"id\">1</field>" +
+ "<field name=\"whitetok\">The Whitetok</field>" +
+ "<field name=\"text\">The Text</field>" +
+ "</doc>" +
+ "</docs>";
+
+ final List<ContentStream> contentStreams = new ArrayList<ContentStream>(1);
+ contentStreams.add(new ContentStreamBase.StringStream(docsInput));
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ params.add("analysis.query", "The Query String");
+ params.add("analysis.showmatch", "true");
+ SolrQueryRequest req = new SolrQueryRequestBase(h.getCore(), params) {
+ @Override
+ public Iterable<ContentStream> getContentStreams() {
+ return contentStreams;
+ }
+ };
+
+ DocumentAnalysisRequest request = handler.resolveAnalysisRequest(req);
+
+ assertNotNull(request);
+ assertTrue(request.isShowMatch());
+ assertNotNull(request.getQuery());
+ assertEquals("The Query String", request.getQuery());
+ List<SolrInputDocument> documents = request.getDocuments();
+ assertNotNull(documents);
+ assertEquals(1, documents.size());
+ SolrInputDocument document = documents.get(0);
+ SolrInputField field = document.getField("id");
+ assertNotNull(field);
+ assertEquals("1", field.getFirstValue());
+ field = document.getField("whitetok");
+ assertNotNull(field);
+ assertEquals("The Whitetok", field.getFirstValue());
+ field = document.getField("text");
+ assertNotNull(field);
+ assertEquals("The Text", field.getFirstValue());
+ }
+
+ /**
+ * Tests the {@link DocumentAnalysisRequestHandler#handleAnalysisRequest(org.apache.solr.client.solrj.request.DocumentAnalysisRequest,
+ * org.apache.solr.schema.IndexSchema)}
+ */
+ public void testHandleAnalysisRequest() throws Exception {
+
+ SolrInputDocument document = new SolrInputDocument();
+ document.addField("id", 1);
+ document.addField("whitetok", "Jumping Jack");
+ document.addField("text", "The Fox Jumped Over The Dogs");
+
+ DocumentAnalysisRequest request = new DocumentAnalysisRequest()
+ .setQuery("JUMPING")
+ .setShowMatch(true)
+ .addDocument(document);
+
+ NamedList<Object> result = handler.handleAnalysisRequest(request, h.getCore().getSchema());
+ assertNotNull("result is null and it shouldn't be", result);
+ NamedList<NamedList<NamedList<Object>>> documentResult = (NamedList<NamedList<NamedList<Object>>>) result.get("1");
+ assertNotNull("An analysis for document with key '1' should be returned", documentResult);
+
+ // the id field
+ NamedList<NamedList<Object>> idResult = documentResult.get("id");
+ assertNotNull("an analysis for the 'id' field should be returned", idResult);
+ NamedList<Object> queryResult = idResult.get("query");
+ assertEquals("Only the default analyzer should be applied", 1, queryResult.size());
+ String name = queryResult.getName(0);
+ assertTrue("Only the default analyzer should be applied", name.matches("org.apache.solr.schema.FieldType\\$DefaultAnalyzer.*"));
+ List<NamedList> tokenList = (List<NamedList>) queryResult.getVal(0);
+ assertEquals("Query has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("JUMPING", null, "word", 0, 7, 1, null, false));
+ NamedList<Object> indexResult = idResult.get("index");
+ assertEquals("The id field has only a single value", 1, indexResult.size());
+ NamedList<List<NamedList>> valueResult = (NamedList<List<NamedList>>) indexResult.get("1");
+ assertEquals("Only the default analyzer should be applied", 1, valueResult.size());
+ name = queryResult.getName(0);
+ assertTrue("Only the default analyzer should be applied", name.matches("org.apache.solr.schema.FieldType\\$DefaultAnalyzer.*"));
+ tokenList = valueResult.getVal(0);
+ assertEquals("The 'id' field value has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("1", null, "word", 0, 1, 1, null, false));
+
+ // the name field
+ NamedList<NamedList<Object>> whitetokResult = documentResult.get("whitetok");
+ assertNotNull("an analysis for the 'whitetok' field should be returned", whitetokResult);
+ queryResult = whitetokResult.get("query");
+ tokenList = (List<NamedList>) queryResult.get("org.apache.lucene.analysis.WhitespaceTokenizer");
+ assertNotNull("Expecting the 'WhitespaceTokenizer' to be applied on the query for the 'whitetok' field", tokenList);
+ assertEquals("Query has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("JUMPING", null, "word", 0, 7, 1, null, false));
+ indexResult = whitetokResult.get("index");
+ assertEquals("The 'whitetok' field has only a single value", 1, indexResult.size());
+ valueResult = (NamedList<List<NamedList>>) indexResult.get("Jumping Jack");
+ tokenList = valueResult.getVal(0);
+ assertEquals("Expecting 2 tokens to be present", 2, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("Jumping", null, "word", 0, 7, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("Jack", null, "word", 8, 12, 2, null, false));
+
+ // the text field
+ NamedList<NamedList<Object>> textResult = documentResult.get("text");
+ assertNotNull("an analysis for the 'text' field should be returned", textResult);
+ queryResult = textResult.get("query");
+ tokenList = (List<NamedList>) queryResult.get("org.apache.lucene.analysis.standard.StandardTokenizer");
+ assertNotNull("Expecting the 'StandardTokenizer' to be applied on the query for the 'text' field", tokenList);
+ assertEquals("Query has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("JUMPING", null, "<ALPHANUM>", 0, 7, 1, null, false));
+ tokenList = (List<NamedList>) queryResult.get("org.apache.lucene.analysis.standard.StandardFilter");
+ assertNotNull("Expecting the 'StandardFilter' to be applied on the query for the 'text' field", tokenList);
+ assertEquals("Query has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("JUMPING", null, "<ALPHANUM>", 0, 7, 1, null, false));
+ tokenList = (List<NamedList>) queryResult.get("org.apache.lucene.analysis.LowerCaseFilter");
+ assertNotNull("Expecting the 'LowerCaseFilter' to be applied on the query for the 'text' field", tokenList);
+ assertEquals("Query has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("jumping", null, "<ALPHANUM>", 0, 7, 1, null, false));
+ tokenList = (List<NamedList>) queryResult.get("org.apache.lucene.analysis.StopFilter");
+ assertNotNull("Expecting the 'StopFilter' to be applied on the query for the 'text' field", tokenList);
+ assertEquals("Query has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("jumping", null, "<ALPHANUM>", 0, 7, 1, null, false));
+ tokenList = (List<NamedList>) queryResult.get("org.apache.solr.analysis.EnglishPorterFilter");
+ assertNotNull("Expecting the 'EnglishPorterFilter' to be applied on the query for the 'text' field", tokenList);
+ assertEquals("Query has only one token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("jump", null, "<ALPHANUM>", 0, 7, 1, null, false));
+ indexResult = textResult.get("index");
+ assertEquals("The 'text' field has only a single value", 1, indexResult.size());
+ valueResult = (NamedList<List<NamedList>>) indexResult.get("The Fox Jumped Over The Dogs");
+ tokenList = valueResult.get("org.apache.lucene.analysis.standard.StandardTokenizer");
+ assertNotNull("Expecting the 'StandardTokenizer' to be applied on the index for the 'text' field", tokenList);
+ assertEquals("Expecting 6 tokens", 6, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("The", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("Fox", null, "<ALPHANUM>", 4, 7, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("Jumped", null, "<ALPHANUM>", 8, 14, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("Over", null, "<ALPHANUM>", 15, 19, 4, null, false));
+ assertToken(tokenList.get(4), new TokenInfo("The", null, "<ALPHANUM>", 20, 23, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("Dogs", null, "<ALPHANUM>", 24, 28, 6, null, false));
+ tokenList = valueResult.get("org.apache.lucene.analysis.standard.StandardFilter");
+ assertNotNull("Expecting the 'StandardFilter' to be applied on the index for the 'text' field", tokenList);
+ assertEquals("Expecting 6 tokens", 6, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("The", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("Fox", null, "<ALPHANUM>", 4, 7, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("Jumped", null, "<ALPHANUM>", 8, 14, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("Over", null, "<ALPHANUM>", 15, 19, 4, null, false));
+ assertToken(tokenList.get(4), new TokenInfo("The", null, "<ALPHANUM>", 20, 23, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("Dogs", null, "<ALPHANUM>", 24, 28, 6, null, false));
+ tokenList = valueResult.get("org.apache.lucene.analysis.LowerCaseFilter");
+ assertNotNull("Expecting the 'LowerCaseFilter' to be applied on the index for the 'text' field", tokenList);
+ assertEquals("Expecting 6 tokens", 6, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("the", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("fox", null, "<ALPHANUM>", 4, 7, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("jumped", null, "<ALPHANUM>", 8, 14, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("over", null, "<ALPHANUM>", 15, 19, 4, null, false));
+ assertToken(tokenList.get(4), new TokenInfo("the", null, "<ALPHANUM>", 20, 23, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("dogs", null, "<ALPHANUM>", 24, 28, 6, null, false));
+ tokenList = valueResult.get("org.apache.lucene.analysis.StopFilter");
+ assertNotNull("Expecting the 'StopFilter' to be applied on the index for the 'text' field", tokenList);
+ assertEquals("Expecting 4 tokens after stop word removal", 4, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "<ALPHANUM>", 4, 7, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("jumped", null, "<ALPHANUM>", 8, 14, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("over", null, "<ALPHANUM>", 15, 19, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("dogs", null, "<ALPHANUM>", 24, 28, 4, null, false));
+ tokenList = valueResult.get("org.apache.solr.analysis.EnglishPorterFilter");
+ assertNotNull("Expecting the 'EnglishPorterFilter' to be applied on the index for the 'text' field", tokenList);
+ assertEquals("Expecting 4 tokens", 4, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "<ALPHANUM>", 4, 7, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("jump", null, "<ALPHANUM>", 8, 14, 2, null, true));
+ assertToken(tokenList.get(2), new TokenInfo("over", null, "<ALPHANUM>", 15, 19, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("dog", null, "<ALPHANUM>", 24, 28, 4, null, false));
+ }
+}
Propchange: lucene/solr/trunk/src/test/org/apache/solr/handler/DocumentAnalysisRequestHandlerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/solr/trunk/src/test/org/apache/solr/handler/DocumentAnalysisRequestHandlerTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: lucene/solr/trunk/src/test/org/apache/solr/handler/FieldAnalysisRequestHandlerTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/handler/FieldAnalysisRequestHandlerTest.java?rev=767412&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/handler/FieldAnalysisRequestHandlerTest.java (added)
+++ lucene/solr/trunk/src/test/org/apache/solr/handler/FieldAnalysisRequestHandlerTest.java Wed Apr 22 08:53:28 2009
@@ -0,0 +1,295 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.handler;
+
+import org.apache.lucene.analysis.KeywordTokenizer;
+import org.apache.lucene.analysis.WhitespaceTokenizer;
+import org.apache.solr.common.params.AnalysisParams;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.client.solrj.request.FieldAnalysisRequest;
+
+import java.util.List;
+
+/**
+ * A test for {@link FieldAnalysisRequestHandler}.
+ *
+ * @version $Id$
+ * @since solr 1.4
+ */
+public class FieldAnalysisRequestHandlerTest extends AnalysisRequestHandlerTestBase {
+
+ private FieldAnalysisRequestHandler handler;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ handler = new FieldAnalysisRequestHandler();
+ }
+
+ @Override
+ public String getSchemaFile() {
+ return "schema.xml";
+ }
+
+ @Override
+ public String getSolrConfigFile() {
+ return "solrconfig.xml";
+ }
+
+ /**
+ * Tests the {@link FieldAnalysisRequestHandler#resolveAnalysisRequest(org.apache.solr.common.params.SolrParams)}
+ */
+ public void testResolveAnalysisRequest() throws Exception {
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ params.add(AnalysisParams.FIELD_NAME, "text,nametext");
+ params.add(AnalysisParams.FIELD_TYPE, "whitetok,keywordtok");
+ params.add(AnalysisParams.FIELD_VALUE, "the quick red fox jumped over the lazy brown dogs");
+ params.add(CommonParams.Q, "fox brown");
+
+ FieldAnalysisRequest request = handler.resolveAnalysisRequest(params);
+ List<String> fieldNames = request.getFieldNames();
+ assertEquals("Expecting 2 field names", 2, fieldNames.size());
+ assertEquals("text", fieldNames.get(0));
+ assertEquals("nametext", fieldNames.get(1));
+ List<String> fieldTypes = request.getFieldTypes();
+ assertEquals("Expecting 2 field types", 2, fieldTypes.size());
+ assertEquals("whitetok", fieldTypes.get(0));
+ assertEquals("keywordtok", fieldTypes.get(1));
+ assertEquals("the quick red fox jumped over the lazy brown dogs", request.getFieldValue());
+ assertEquals("fox brown", request.getQuery());
+ assertFalse(request.isShowMatch());
+
+ // testing overide of query value using analysis.query param
+ params.add(AnalysisParams.QUERY, "quick lazy");
+ request = handler.resolveAnalysisRequest(params);
+ assertEquals("quick lazy", request.getQuery());
+
+ // testing analysis.showmatch param
+ params.add(AnalysisParams.SHOW_MATCH, "false");
+ request = handler.resolveAnalysisRequest(params);
+ assertFalse(request.isShowMatch());
+ params.set(AnalysisParams.SHOW_MATCH, "true");
+ request = handler.resolveAnalysisRequest(params);
+ assertTrue(request.isShowMatch());
+
+ // testing absence of query value
+ params.remove(CommonParams.Q);
+ params.remove(AnalysisParams.QUERY);
+ request = handler.resolveAnalysisRequest(params);
+ assertNull(request.getQuery());
+ }
+
+ /**
+ * Tests the {@link FieldAnalysisRequestHandler#handleAnalysisRequest(org.apache.solr.client.solrj.request.FieldAnalysisRequest,
+ * org.apache.solr.schema.IndexSchema)}
+ */
+ public void testHandleAnalysisRequest() throws Exception {
+
+ FieldAnalysisRequest request = new FieldAnalysisRequest();
+ request.addFieldName("whitetok");
+ request.addFieldName("keywordtok");
+ request.addFieldType("text");
+ request.addFieldType("nametext");
+ request.setFieldValue("the quick red fox jumped over the lazy brown dogs");
+ request.setQuery("fox brown");
+ request.setShowMatch(true);
+
+ NamedList<NamedList> result = handler.handleAnalysisRequest(request, h.getCore().getSchema());
+ assertTrue("result is null and it shouldn't be", result != null);
+
+ NamedList<NamedList> fieldTypes = result.get("field_types");
+ assertNotNull("field_types should never be null", fieldTypes);
+ NamedList<NamedList> textType = fieldTypes.get("text");
+ assertNotNull("expecting result for field type 'text'", textType);
+
+ NamedList<List<NamedList>> indexPart = textType.get("index");
+ assertNotNull("expecting an index token analysis for field type 'text'", indexPart);
+
+ List<NamedList> tokenList = indexPart.get("org.apache.lucene.analysis.standard.StandardTokenizer");
+ assertNotNull("Expcting StandardTokenizer analysis breakdown", tokenList);
+ assertEquals(tokenList.size(), 10);
+ assertToken(tokenList.get(0), new TokenInfo("the", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("quick", null, "<ALPHANUM>", 4, 9, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("red", null, "<ALPHANUM>", 10, 13, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("fox", null, "<ALPHANUM>", 14, 17, 4, null, true));
+ assertToken(tokenList.get(4), new TokenInfo("jumped", null, "<ALPHANUM>", 18, 24, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("over", null, "<ALPHANUM>", 25, 29, 6, null, false));
+ assertToken(tokenList.get(6), new TokenInfo("the", null, "<ALPHANUM>", 30, 33, 7, null, false));
+ assertToken(tokenList.get(7), new TokenInfo("lazy", null, "<ALPHANUM>", 34, 38, 8, null, false));
+ assertToken(tokenList.get(8), new TokenInfo("brown", null, "<ALPHANUM>", 39, 44, 9, null, true));
+ assertToken(tokenList.get(9), new TokenInfo("dogs", null, "<ALPHANUM>", 45, 49, 10, null, false));
+ tokenList = indexPart.get("org.apache.lucene.analysis.standard.StandardFilter");
+ assertNotNull("Expcting StandardFilter analysis breakdown", tokenList);
+ assertEquals(tokenList.size(), 10);
+ assertToken(tokenList.get(0), new TokenInfo("the", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("quick", null, "<ALPHANUM>", 4, 9, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("red", null, "<ALPHANUM>", 10, 13, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("fox", null, "<ALPHANUM>", 14, 17, 4, null, true));
+ assertToken(tokenList.get(4), new TokenInfo("jumped", null, "<ALPHANUM>", 18, 24, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("over", null, "<ALPHANUM>", 25, 29, 6, null, false));
+ assertToken(tokenList.get(6), new TokenInfo("the", null, "<ALPHANUM>", 30, 33, 7, null, false));
+ assertToken(tokenList.get(7), new TokenInfo("lazy", null, "<ALPHANUM>", 34, 38, 8, null, false));
+ assertToken(tokenList.get(8), new TokenInfo("brown", null, "<ALPHANUM>", 39, 44, 9, null, true));
+ assertToken(tokenList.get(9), new TokenInfo("dogs", null, "<ALPHANUM>", 45, 49, 10, null, false));
+ tokenList = indexPart.get("org.apache.lucene.analysis.LowerCaseFilter");
+ assertNotNull("Expcting LowerCaseFilter analysis breakdown", tokenList);
+ assertEquals(tokenList.size(), 10);
+ assertToken(tokenList.get(0), new TokenInfo("the", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("quick", null, "<ALPHANUM>", 4, 9, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("red", null, "<ALPHANUM>", 10, 13, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("fox", null, "<ALPHANUM>", 14, 17, 4, null, true));
+ assertToken(tokenList.get(4), new TokenInfo("jumped", null, "<ALPHANUM>", 18, 24, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("over", null, "<ALPHANUM>", 25, 29, 6, null, false));
+ assertToken(tokenList.get(6), new TokenInfo("the", null, "<ALPHANUM>", 30, 33, 7, null, false));
+ assertToken(tokenList.get(7), new TokenInfo("lazy", null, "<ALPHANUM>", 34, 38, 8, null, false));
+ assertToken(tokenList.get(8), new TokenInfo("brown", null, "<ALPHANUM>", 39, 44, 9, null, true));
+ assertToken(tokenList.get(9), new TokenInfo("dogs", null, "<ALPHANUM>", 45, 49, 10, null, false));
+ tokenList = indexPart.get("org.apache.lucene.analysis.StopFilter");
+ assertNotNull("Expcting StopFilter analysis breakdown", tokenList);
+ assertEquals(tokenList.size(), 8);
+ assertToken(tokenList.get(0), new TokenInfo("quick", null, "<ALPHANUM>", 4, 9, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("red", null, "<ALPHANUM>", 10, 13, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("fox", null, "<ALPHANUM>", 14, 17, 3, null, true));
+ assertToken(tokenList.get(3), new TokenInfo("jumped", null, "<ALPHANUM>", 18, 24, 4, null, false));
+ assertToken(tokenList.get(4), new TokenInfo("over", null, "<ALPHANUM>", 25, 29, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("lazy", null, "<ALPHANUM>", 34, 38, 6, null, false));
+ assertToken(tokenList.get(6), new TokenInfo("brown", null, "<ALPHANUM>", 39, 44, 7, null, true));
+ assertToken(tokenList.get(7), new TokenInfo("dogs", null, "<ALPHANUM>", 45, 49, 8, null, false));
+ tokenList = indexPart.get("org.apache.solr.analysis.EnglishPorterFilter");
+ assertNotNull("Expcting EnglishPorterFilter analysis breakdown", tokenList);
+ assertEquals(tokenList.size(), 8);
+ assertToken(tokenList.get(0), new TokenInfo("quick", null, "<ALPHANUM>", 4, 9, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("red", null, "<ALPHANUM>", 10, 13, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("fox", null, "<ALPHANUM>", 14, 17, 3, null, true));
+ assertToken(tokenList.get(3), new TokenInfo("jump", null, "<ALPHANUM>", 18, 24, 4, null, false));
+ assertToken(tokenList.get(4), new TokenInfo("over", null, "<ALPHANUM>", 25, 29, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("lazi", null, "<ALPHANUM>", 34, 38, 6, null, false));
+ assertToken(tokenList.get(6), new TokenInfo("brown", null, "<ALPHANUM>", 39, 44, 7, null, true));
+ assertToken(tokenList.get(7), new TokenInfo("dog", null, "<ALPHANUM>", 45, 49, 8, null, false));
+
+ NamedList<List<NamedList>> queryPart = textType.get("query");
+ assertNotNull("expecting a query token analysis for field type 'text'", queryPart);
+
+ tokenList = queryPart.get("org.apache.lucene.analysis.standard.StandardTokenizer");
+ assertNotNull("Expecting StandardTokenizer analysis breakdown", tokenList);
+ assertEquals("Expecting StandardTokenizer to produce 2 tokens from '" + request.getQuery() + "'", 2, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("brown", null, "<ALPHANUM>", 4, 9, 2, null, false));
+ tokenList = queryPart.get("org.apache.lucene.analysis.standard.StandardFilter");
+ assertNotNull("Expcting StandardFilter analysis breakdown", tokenList);
+ assertEquals(2, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("brown", null, "<ALPHANUM>", 4, 9, 2, null, false));
+ tokenList = queryPart.get("org.apache.lucene.analysis.LowerCaseFilter");
+ assertNotNull("Expcting LowerCaseFilter analysis breakdown", tokenList);
+ assertEquals(2, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("brown", null, "<ALPHANUM>", 4, 9, 2, null, false));
+ tokenList = queryPart.get("org.apache.lucene.analysis.StopFilter");
+ assertNotNull("Expcting StopFilter analysis breakdown", tokenList);
+ assertEquals(2, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("brown", null, "<ALPHANUM>", 4, 9, 2, null, false));
+ tokenList = queryPart.get("org.apache.solr.analysis.EnglishPorterFilter");
+ assertNotNull("Expcting EnglishPorterFilter analysis breakdown", tokenList);
+ assertEquals(2, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "<ALPHANUM>", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("brown", null, "<ALPHANUM>", 4, 9, 2, null, false));
+
+ NamedList<NamedList> nameTextType = fieldTypes.get("nametext");
+ assertNotNull("expecting result for field type 'nametext'", nameTextType);
+
+ indexPart = nameTextType.get("index");
+ assertNotNull("expecting an index token analysis for field type 'nametext'", indexPart);
+
+ tokenList = indexPart.get("org.apache.lucene.analysis.WhitespaceTokenizer");
+ assertNotNull("Expcting WhitespaceTokenizer analysis breakdown", tokenList);
+ assertEquals(10, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("the", null, "word", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("quick", null, "word", 4, 9, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("red", null, "word", 10, 13, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("fox", null, "word", 14, 17, 4, null, true));
+ assertToken(tokenList.get(4), new TokenInfo("jumped", null, "word", 18, 24, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("over", null, "word", 25, 29, 6, null, false));
+ assertToken(tokenList.get(6), new TokenInfo("the", null, "word", 30, 33, 7, null, false));
+ assertToken(tokenList.get(7), new TokenInfo("lazy", null, "word", 34, 38, 8, null, false));
+ assertToken(tokenList.get(8), new TokenInfo("brown", null, "word", 39, 44, 9, null, true));
+ assertToken(tokenList.get(9), new TokenInfo("dogs", null, "word", 45, 49, 10, null, false));
+
+ queryPart = nameTextType.get("query");
+ assertNotNull("expecting a query token analysis for field type 'nametext'", queryPart);
+ tokenList = queryPart.get(WhitespaceTokenizer.class.getName());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "word", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("brown", null, "word", 4, 9, 2, null, false));
+
+ NamedList<NamedList> fieldNames = result.get("field_names");
+ assertNotNull("field_nameds should never be null", fieldNames);
+
+ NamedList<NamedList> whitetok = fieldNames.get("whitetok");
+ assertNotNull("expecting result for field 'whitetok'", whitetok);
+
+ indexPart = whitetok.get("index");
+ assertNotNull("expecting an index token analysis for field 'whitetok'", indexPart);
+ assertEquals("expecting only WhitespaceTokenizer to be applied", 1, indexPart.size());
+ tokenList = indexPart.get(WhitespaceTokenizer.class.getName());
+ assertNotNull("expecting only WhitespaceTokenizer to be applied", tokenList);
+ assertEquals("expecting WhitespaceTokenizer to produce 10 tokens", 10, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("the", null, "word", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("quick", null, "word", 4, 9, 2, null, false));
+ assertToken(tokenList.get(2), new TokenInfo("red", null, "word", 10, 13, 3, null, false));
+ assertToken(tokenList.get(3), new TokenInfo("fox", null, "word", 14, 17, 4, null, true));
+ assertToken(tokenList.get(4), new TokenInfo("jumped", null, "word", 18, 24, 5, null, false));
+ assertToken(tokenList.get(5), new TokenInfo("over", null, "word", 25, 29, 6, null, false));
+ assertToken(tokenList.get(6), new TokenInfo("the", null, "word", 30, 33, 7, null, false));
+ assertToken(tokenList.get(7), new TokenInfo("lazy", null, "word", 34, 38, 8, null, false));
+ assertToken(tokenList.get(8), new TokenInfo("brown", null, "word", 39, 44, 9, null, true));
+ assertToken(tokenList.get(9), new TokenInfo("dogs", null, "word", 45, 49, 10, null, false));
+
+ queryPart = whitetok.get("query");
+ assertNotNull("expecting a query token analysis for field 'whitetok'", queryPart);
+ assertEquals("expecting only WhitespaceTokenizer to be applied", 1, queryPart.size());
+ tokenList = queryPart.get(WhitespaceTokenizer.class.getName());
+ assertNotNull("expecting only WhitespaceTokenizer to be applied", tokenList);
+ assertEquals("expecting WhitespaceTokenizer to produce 2 tokens", 2, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox", null, "word", 0, 3, 1, null, false));
+ assertToken(tokenList.get(1), new TokenInfo("brown", null, "word", 4, 9, 2, null, false));
+
+ NamedList<NamedList> keywordtok = fieldNames.get("keywordtok");
+ assertNotNull("expecting result for field 'keywordtok'", keywordtok);
+
+ indexPart = keywordtok.get("index");
+ assertNotNull("expecting an index token analysis for field 'keywordtok'", indexPart);
+ assertEquals("expecting only KeywordTokenizer to be applied", 1, indexPart.size());
+ tokenList = indexPart.get(KeywordTokenizer.class.getName());
+ assertNotNull("expecting only KeywordTokenizer to be applied", tokenList);
+ assertEquals("expecting KeywordTokenizer to produce 1 token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("the quick red fox jumped over the lazy brown dogs", null, "word", 0, 49, 1, null, false));
+
+ queryPart = keywordtok.get("query");
+ assertNotNull("expecting a query token analysis for field 'keywordtok'", queryPart);
+ assertEquals("expecting only KeywordTokenizer to be applied", 1, queryPart.size());
+ tokenList = queryPart.get(KeywordTokenizer.class.getName());
+ assertNotNull("expecting only KeywordTokenizer to be applied", tokenList);
+ assertEquals("expecting KeywordTokenizer to produce 1 token", 1, tokenList.size());
+ assertToken(tokenList.get(0), new TokenInfo("fox brown", null, "word", 0, 9, 1, null, false));
+
+ }
+
+}
Propchange: lucene/solr/trunk/src/test/org/apache/solr/handler/FieldAnalysisRequestHandlerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/solr/trunk/src/test/org/apache/solr/handler/FieldAnalysisRequestHandlerTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL