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 org.apache.logging.log4j.LogManager;
19  import org.apache.logging.log4j.Logger;
20  import org.apache.lucene.search.BoostQuery;
21  import org.apache.lucene.search.Query;
22  import org.codelibs.fess.entity.QueryContext;
23  import org.codelibs.fess.exception.InvalidQueryException;
24  import org.lastaflute.core.message.UserMessages;
25  import org.opensearch.index.query.QueryBuilder;
26  
27  /**
28   * Query command implementation for handling Boost queries.
29   * Processes Lucene BoostQuery objects and applies boost factors.
30   */
31  public class BoostQueryCommand extends QueryCommand {
32      /**
33       * Default constructor for BoostQueryCommand.
34       */
35      public BoostQueryCommand() {
36          super();
37      }
38  
39      private static final Logger logger = LogManager.getLogger(BoostQueryCommand.class);
40  
41      @Override
42      protected String getQueryClassName() {
43          return BoostQuery.class.getSimpleName();
44      }
45  
46      @Override
47      public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
48          if (query instanceof final BoostQuery boostQuery) {
49              if (logger.isDebugEnabled()) {
50                  logger.debug("BoostQuery: query={}, boost={}", query, boost);
51              }
52              return getQueryProcessor().execute(context, boostQuery.getQuery(), boostQuery.getBoost());
53          }
54          throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
55                  "Unknown q: " + query.getClass() + " => " + query);
56      }
57  
58  }