View Javadoc
1   /*
2    * Copyright 2012-2021 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.indexer;
17  
18  import java.util.Map;
19  
20  import org.codelibs.fess.Constants;
21  import org.codelibs.fess.es.config.exentity.BoostDocumentRule;
22  import org.codelibs.fess.util.ComponentUtil;
23  
24  public class DocBoostMatcher {
25  
26      private String boostExpression = "0";
27  
28      private String matchExpression;
29  
30      private String scriptType;
31  
32      public DocBoostMatcher() {
33          scriptType = Constants.DEFAULT_SCRIPT;
34      }
35  
36      public DocBoostMatcher(final BoostDocumentRule rule) {
37          matchExpression = rule.getUrlExpr();
38          boostExpression = rule.getBoostExpr();
39          scriptType = ComponentUtil.getFessConfig().getCrawlerDefaultScript();
40      }
41  
42      public boolean match(final Map<String, Object> map) {
43  
44          if (map == null || map.isEmpty() || matchExpression == null) {
45              return false;
46          }
47  
48          final Object value = ComponentUtil.getScriptEngineFactory().getScriptEngine(scriptType).evaluate(matchExpression, map);
49          if (value instanceof Boolean) {
50              return ((Boolean) value);
51          }
52  
53          return false;
54      }
55  
56      public float getValue(final Map<String, Object> map) {
57          if (map == null || map.isEmpty()) {
58              return 0.0f;
59          }
60  
61          final Object value = ComponentUtil.getScriptEngineFactory().getScriptEngine(scriptType).evaluate(boostExpression, map);
62          if (value instanceof Integer) {
63              return ((Integer) value).floatValue();
64          }
65          if (value instanceof Long) {
66              return ((Long) value).floatValue();
67          }
68          if (value instanceof Float) {
69              return ((Float) value);
70          } else if (value instanceof Double) {
71              return ((Double) value).floatValue();
72          } else if (value != null) {
73              return Float.parseFloat(value.toString());
74          }
75  
76          return 0.0f;
77      }
78  
79      public String getBoostExpression() {
80          return boostExpression;
81      }
82  
83      public void setBoostExpression(final String expression) {
84          boostExpression = expression;
85      }
86  
87      public String getMatchExpression() {
88          return matchExpression;
89      }
90  
91      public void setMatchExpression(final String expression) {
92          matchExpression = expression;
93      }
94  
95  }