/solr/core/src/test/org/apache/solr/update/processor/PreAnalyzedUpdateProcessorTest.java

https://github.com/apache/solr · Java · 122 lines · 96 code · 8 blank · 18 comment · 0 complexity · f68f90656a770a698c4877633d7b3c4d MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.solr.update.processor;
  18. import org.apache.lucene.document.Field;
  19. import org.apache.solr.common.SolrInputDocument;
  20. import org.junit.BeforeClass;
  21. import org.junit.Test;
  22. public class PreAnalyzedUpdateProcessorTest extends UpdateProcessorTestBase {
  23. String[] simpleTitle = new String[] {"not pre-analyzed", "1 =string value=foo bar"};
  24. String[] jsonTitle =
  25. new String[] {
  26. "not pre-analyzed",
  27. "{\"v\":\"1\",\"str\":\"string value\",\"tokens\":[{\"t\":\"foo\"},{\"t\":\"bar\"}]}",
  28. };
  29. String[] simpleTeststop =
  30. new String[] {"1 =this is a test.=one two three", "1 =this is a test.=three four five"};
  31. String[] jsonTeststop =
  32. new String[] {
  33. "{\"v\":\"1\",\"str\":\"this is a test.\",\"tokens\":[{\"t\":\"one\"},{\"t\":\"two\"},{\"t\":\"three\"}]}",
  34. "{\"v\":\"1\",\"str\":\"this is a test.\",\"tokens\":[{\"t\":\"three\"},{\"t\":\"four\"},{\"t\":\"five\"}]}",
  35. };
  36. @BeforeClass
  37. public static void beforeClass() throws Exception {
  38. initCore("solrconfig-update-processor-chains.xml", "schema12.xml");
  39. }
  40. @Test
  41. public void testSimple() throws Exception {
  42. test("pre-analyzed-simple", simpleTitle, simpleTeststop);
  43. }
  44. @Test
  45. public void testJson() throws Exception {
  46. test("pre-analyzed-json", jsonTitle, jsonTeststop);
  47. }
  48. private void test(String chain, String[] title, String[] teststop) throws Exception {
  49. SolrInputDocument doc =
  50. processAdd(
  51. chain,
  52. doc(
  53. f("id", "1"),
  54. f("title", title[0]),
  55. f("teststop", teststop[0]),
  56. f("nonexistent", "foobar"),
  57. f("ssto", teststop[0]),
  58. f("sind", teststop[0])));
  59. assertEquals("title should be unchanged", title[0], doc.getFieldValue("title"));
  60. assertTrue("teststop should be a Field", doc.getFieldValue("teststop") instanceof Field);
  61. Field f = (Field) doc.getFieldValue("teststop");
  62. assertEquals("teststop should have stringValue", "this is a test.", f.stringValue());
  63. assertNotNull("teststop should have tokensStreamValue", f.tokenStreamValue());
  64. assertNull("nonexistent should be dropped", doc.getField("nonexistent"));
  65. // check how SchemaField type affects stored/indexed part processing
  66. f = (Field) doc.getFieldValue("ssto");
  67. assertNotNull("should have ssto", f);
  68. assertNotNull("should have stringValue", f.stringValue());
  69. assertNull("should not have tokenStreamValue", f.tokenStreamValue());
  70. f = (Field) doc.getFieldValue("sind");
  71. assertNotNull("should have sind", f);
  72. assertNull("should not have stringValue: '" + f.stringValue() + "'", f.stringValue());
  73. assertNotNull("should have tokenStreamValue", f.tokenStreamValue());
  74. doc =
  75. processAdd(
  76. chain,
  77. doc(
  78. f("id", "2"),
  79. f("title", title[1]),
  80. f("teststop", teststop[1]),
  81. f("nonexistent", "foobar"),
  82. f("ssto", teststop[1]),
  83. f("sind", teststop[1])));
  84. assertTrue("title should be a Field", doc.getFieldValue("title") instanceof Field);
  85. assertTrue("teststop should be a Field", doc.getFieldValue("teststop") instanceof Field);
  86. f = (Field) doc.getFieldValue("teststop");
  87. assertEquals("teststop should have stringValue", "this is a test.", f.stringValue());
  88. assertNotNull("teststop should have tokensStreamValue", f.tokenStreamValue());
  89. assertNull("nonexistent should be dropped", doc.getField("nonexistent"));
  90. // check how SchemaField type affects stored/indexed part processing
  91. f = (Field) doc.getFieldValue("ssto");
  92. assertNotNull("should have ssto", f);
  93. assertNotNull("should have stringValue", f.stringValue());
  94. assertNull("should not have tokenStreamValue", f.tokenStreamValue());
  95. f = (Field) doc.getFieldValue("sind");
  96. assertNotNull("should have sind", f);
  97. assertNull("should not have stringValue: '" + f.stringValue() + "'", f.stringValue());
  98. assertNotNull("should have tokenStreamValue", f.tokenStreamValue());
  99. assertU(commit());
  100. assertQ(
  101. req("teststop:\"one two three\""),
  102. "//str[@name='id'][.='1']",
  103. "//str[@name='teststop'][.='this is a test.']");
  104. assertQ(
  105. req("teststop:three"),
  106. "//*[@numFound='2']",
  107. "//result/doc[1]/str[@name='id'][.='1']",
  108. "//result/doc[1]/str[@name='title'][.='not pre-analyzed']",
  109. "//result/doc[2]/str[@name='id'][.='2']",
  110. "//result/doc[2]/arr[@name='title']/str[.='string value']");
  111. assertQ(req("ssto:three"), "//*[@numFound='0']");
  112. assertQ(req("sind:three"), "//*[@numFound='2']");
  113. }
  114. }