View Javadoc
1   /*
2    * Copyright 2012-2025 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  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.apache.lucene.index.Term;
23  import org.apache.lucene.search.PhraseQuery;
24  import org.apache.lucene.search.Query;
25  import org.codelibs.fess.Constants;
26  import org.codelibs.fess.entity.QueryContext;
27  import org.codelibs.fess.exception.InvalidQueryException;
28  import org.codelibs.fess.mylasta.direction.FessConfig;
29  import org.codelibs.fess.util.ComponentUtil;
30  import org.lastaflute.core.message.UserMessages;
31  import org.opensearch.index.query.QueryBuilder;
32  
33  /**
34   * Query command for phrase queries.
35   */
36  public class PhraseQueryCommand extends QueryCommand {
37  
38      /**
39       * Default constructor.
40       */
41      public PhraseQueryCommand() {
42          super();
43      }
44  
45      private static final Logger logger = LogManager.getLogger(PhraseQueryCommand.class);
46  
47      @Override
48      protected String getQueryClassName() {
49          return PhraseQuery.class.getSimpleName();
50      }
51  
52      @Override
53      public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
54          if (query instanceof final PhraseQuery phraseQuery) {
55              if (logger.isDebugEnabled()) {
56                  logger.debug("PhraseQuery: query={}, boost={}", query, boost);
57              }
58              return convertPhraseQuery(context, phraseQuery, boost);
59          }
60          throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
61                  "Unknown q: " + query.getClass() + " => " + query);
62      }
63  
64      /**
65       * Converts a phrase query to a query builder.
66       *
67       * @param context the query context
68       * @param phraseQuery the phrase query
69       * @param boost the boost factor
70       * @return the query builder
71       */
72      protected QueryBuilder convertPhraseQuery(final QueryContext context, final PhraseQuery phraseQuery, final float boost) {
73          final Term[] terms = phraseQuery.getTerms();
74          if (terms.length == 0) {
75              throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
76                      "Unknown phrase query: " + phraseQuery);
77          }
78          final FessConfig fessConfig = ComponentUtil.getFessConfig();
79          final String field = terms[0].field();
80          final String[] texts = stream(terms).get(stream -> stream.map(Term::text).toArray(n -> new String[n]));
81  
82          return convertPhraseQuery(fessConfig, context, phraseQuery, boost, field, texts);
83      }
84  
85      /**
86       * Converts a phrase query to a query builder with specified field and texts.
87       *
88       * @param fessConfig the Fess configuration
89       * @param context the query context
90       * @param phraseQuery the phrase query
91       * @param boost the boost factor
92       * @param field the field name
93       * @param texts the query texts
94       * @return the query builder
95       */
96      protected QueryBuilder convertPhraseQuery(final FessConfig fessConfig, final QueryContext context, final PhraseQuery phraseQuery,
97              final float boost, final String field, final String[] texts) {
98          final String text = String.join(" ", texts);
99  
100         if (Constants.DEFAULT_FIELD.equals(field)) {
101             context.addFieldLog(field, text);
102             stream(texts).of(stream -> stream.forEach(t -> context.addHighlightedQuery(t)));
103             return buildDefaultQueryBuilder(fessConfig, context, (f, b) -> buildMatchPhraseQuery(f, text).boost(b * boost));
104         }
105 
106         if (isSearchField(field)) {
107             context.addFieldLog(field, text);
108             stream(texts).of(stream -> stream.forEach(t -> context.addHighlightedQuery(t)));
109             return buildMatchPhraseQuery(field, text);
110         }
111 
112         context.addFieldLog(Constants.DEFAULT_FIELD, text);
113         return buildDefaultQueryBuilder(fessConfig, context, (f, b) -> buildMatchPhraseQuery(f, text).boost(b * boost));
114     }
115 
116 }