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