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