TermQueryCommandTest.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 2012-2024 CodeLibs Project and the Others.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. * either express or implied. See the License for the specific language
  14. * governing permissions and limitations under the License.
  15. */
  16. package org.codelibs.fess.query;
  17. import java.math.BigDecimal;
  18. import java.util.List;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.lucene.search.Query;
  22. import org.apache.lucene.search.TermQuery;
  23. import org.codelibs.fess.entity.QueryContext;
  24. import org.codelibs.fess.exception.InvalidQueryException;
  25. import org.codelibs.fess.mylasta.direction.FessConfig;
  26. import org.codelibs.fess.query.parser.QueryParser;
  27. import org.codelibs.fess.unit.UnitFessTestCase;
  28. import org.codelibs.fess.util.ComponentUtil;
  29. import org.dbflute.util.DfTypeUtil;
  30. import org.opensearch.index.query.BoolQueryBuilder;
  31. import org.opensearch.index.query.MatchPhraseQueryBuilder;
  32. import org.opensearch.index.query.PrefixQueryBuilder;
  33. import org.opensearch.index.query.QueryBuilder;
  34. import org.opensearch.index.query.TermQueryBuilder;
  35. import org.opensearch.index.query.WildcardQueryBuilder;
  36. import org.opensearch.search.sort.SortBuilder;
  37. public class TermQueryCommandTest extends UnitFessTestCase {
  38. private static final Logger logger = LogManager.getLogger(TermQueryCommandTest.class);
  39. private TermQueryCommand queryCommand;
  40. @Override
  41. public void setUp() throws Exception {
  42. super.setUp();
  43. QueryFieldConfig queryFieldConfig = new QueryFieldConfig();
  44. queryFieldConfig.init();
  45. ComponentUtil.register(queryFieldConfig, "queryFieldConfig");
  46. QueryParser queryParser = new QueryParser();
  47. queryParser.init();
  48. ComponentUtil.register(queryParser, "queryParser");
  49. queryCommand = new TermQueryCommand();
  50. }
  51. @Override
  52. public void tearDown() throws Exception {
  53. ComponentUtil.setFessConfig(null);
  54. super.tearDown();
  55. }
  56. private void setQueryType(final String queryType) {
  57. final FessConfig fessConfig = ComponentUtil.getFessConfig();
  58. ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() {
  59. @Override
  60. public String get(String propertyKey) {
  61. return fessConfig.get(propertyKey);
  62. }
  63. @Override
  64. public BigDecimal getAsDecimal(String propertyKey) {
  65. return DfTypeUtil.toBigDecimal(get(propertyKey));
  66. }
  67. @Override
  68. public Integer getAsInteger(String propertyKey) {
  69. return DfTypeUtil.toInteger(get(propertyKey));
  70. }
  71. @Override
  72. public String getQueryDefaultQueryType() {
  73. return queryType;
  74. }
  75. });
  76. }
  77. public void test_convertTermQuery() throws Exception {
  78. setQueryType("bool");
  79. assertQueryBuilder(DefaultQueryBuilder.class,
  80. "{\"bool\":{\"should\":[{\"match_phrase\":{\"title\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.5}}},{\"match_phrase\":{\"content\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.05}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}",
  81. "aaa");
  82. assertQueryBuilder(MatchPhraseQueryBuilder.class,
  83. "{\"match_phrase\":{\"title\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":1.0}}}", //
  84. "title:aaa");
  85. assertQueryBuilder(MatchPhraseQueryBuilder.class,
  86. "{\"match_phrase\":{\"content\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":1.0}}}", //
  87. "content:aaa");
  88. assertQueryBuilder(DefaultQueryBuilder.class,
  89. "{\"bool\":{\"should\":[{\"match_phrase\":{\"title\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.5}}},{\"match_phrase\":{\"content\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.05}}},{\"fuzzy\":{\"title\":{\"value\":\"xxx:aaa\",\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":10,\"transpositions\":true,\"boost\":0.01}}},{\"fuzzy\":{\"content\":{\"value\":\"xxx:aaa\",\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":10,\"transpositions\":true,\"boost\":0.005}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}",
  90. "xxx:aaa");
  91. assertQueryBuilder(WildcardQueryBuilder.class, //
  92. "{\"wildcard\":{\"url\":{\"wildcard\":\"*aaa*\",\"boost\":1.0}}}", //
  93. "inurl:aaa");
  94. assertQueryBuilder(TermQueryBuilder.class, //
  95. "{\"term\":{\"url\":{\"value\":\"aaa\",\"boost\":1.0}}}", //
  96. "url:aaa");
  97. assertQueryBuilder(PrefixQueryBuilder.class, //
  98. "{\"prefix\":{\"site\":{\"value\":\"aaa\",\"boost\":1.0}}}", //
  99. "site:aaa");
  100. assertQueryBuilder("{\"timestamp\":{\"order\":\"asc\"}}", "sort:timestamp");
  101. assertQueryBuilder("{\"timestamp\":{\"order\":\"asc\"}}", "sort:timestamp.asc");
  102. assertQueryBuilder("{\"timestamp\":{\"order\":\"desc\"}}", "sort:timestamp.desc");
  103. try {
  104. assertQueryBuilder("", "sort:xxx");
  105. fail();
  106. } catch (InvalidQueryException e) {
  107. // nothing
  108. }
  109. }
  110. public void test_convertTermQuery_dismax() throws Exception {
  111. setQueryType("dismax");
  112. assertQueryBuilder(DefaultQueryBuilder.class,
  113. "{\"dis_max\":{\"tie_breaker\":0.1,\"queries\":[{\"match_phrase\":{\"title\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.5}}},{\"match_phrase\":{\"content\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.05}}}],\"boost\":1.0}}",
  114. "aaa");
  115. assertQueryBuilder(MatchPhraseQueryBuilder.class,
  116. "{\"match_phrase\":{\"title\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":1.0}}}", //
  117. "title:aaa");
  118. assertQueryBuilder(MatchPhraseQueryBuilder.class,
  119. "{\"match_phrase\":{\"content\":{\"query\":\"aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":1.0}}}", //
  120. "content:aaa");
  121. assertQueryBuilder(DefaultQueryBuilder.class,
  122. "{\"dis_max\":{\"tie_breaker\":0.1,\"queries\":[{\"match_phrase\":{\"title\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.5}}},{\"match_phrase\":{\"content\":{\"query\":\"xxx:aaa\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":0.05}}},{\"fuzzy\":{\"title\":{\"value\":\"xxx:aaa\",\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":10,\"transpositions\":true,\"boost\":0.01}}},{\"fuzzy\":{\"content\":{\"value\":\"xxx:aaa\",\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":10,\"transpositions\":true,\"boost\":0.005}}}],\"boost\":1.0}}",
  123. "xxx:aaa");
  124. assertQueryBuilder(WildcardQueryBuilder.class, //
  125. "{\"wildcard\":{\"url\":{\"wildcard\":\"*aaa*\",\"boost\":1.0}}}", //
  126. "inurl:aaa");
  127. assertQueryBuilder(TermQueryBuilder.class, //
  128. "{\"term\":{\"url\":{\"value\":\"aaa\",\"boost\":1.0}}}", //
  129. "url:aaa");
  130. assertQueryBuilder(PrefixQueryBuilder.class, //
  131. "{\"prefix\":{\"site\":{\"value\":\"aaa\",\"boost\":1.0}}}", //
  132. "site:aaa");
  133. assertQueryBuilder("{\"timestamp\":{\"order\":\"asc\"}}", "sort:timestamp");
  134. assertQueryBuilder("{\"timestamp\":{\"order\":\"asc\"}}", "sort:timestamp.asc");
  135. assertQueryBuilder("{\"timestamp\":{\"order\":\"desc\"}}", "sort:timestamp.desc");
  136. try {
  137. assertQueryBuilder("", "sort:xxx");
  138. fail();
  139. } catch (InvalidQueryException e) {
  140. // nothing
  141. }
  142. }
  143. private void assertQueryBuilder(final String expect, final String text) throws Exception {
  144. QueryContext queryContext = assertQueryBuilder(null, null, text);
  145. List<SortBuilder<?>> sortBuilders = queryContext.sortBuilders();
  146. assertEquals(1, sortBuilders.size());
  147. logger.info("{} => {}", text, sortBuilders.get(0).toString());
  148. assertEquals(expect, sortBuilders.get(0).toString().replaceAll("[\s\n]", ""));
  149. }
  150. private QueryContext assertQueryBuilder(final Class<?> expectedClass, final String expectedQuery, final String text) throws Exception {
  151. final QueryContext queryContext = new QueryContext(text, false);
  152. final Query query = ComponentUtil.getQueryParser().parse(queryContext.getQueryString());
  153. QueryBuilder builder = queryCommand.convertTermQuery(queryContext, (TermQuery) query, 1.0f);
  154. if (text.startsWith("sort:")) {
  155. assertNull(builder);
  156. } else {
  157. logger.info("{} => {}", text, builder.toString());
  158. assertEquals(expectedClass, builder.getClass());
  159. assertEquals(expectedQuery, builder.toString().replaceAll("[\s\n]", ""));
  160. }
  161. return queryContext;
  162. }
  163. }