public
Description: semantic repository service is intended for indexing content from different sources and maintain multi indexes for different types of content and perform different types of search. yet another solr type indexing service on top of lucene but it will gradually support content versioning and more semantic search result.
Homepage: http://hasan.we4tech.com
Clone URL: git://github.com/we4tech/semantic-repository.git
100644 81 lines (70 sloc) 2.953 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* $Id$
* *****************************************************************************
* Copyright (C) 2005 - 2007 somewhere in .Net ltd.
* All Rights Reserved. No use, copying or distribution of this
* work may be made except in accordance with a valid license
* agreement from somewhere in .Net LTD. This notice must be included on
* all copies, modifications and derivatives of this work.
* *****************************************************************************
* $LastChangedBy$
* $LastChangedDate$
* $LastChangedRevision$
* *****************************************************************************
*/
 
package com.ideabase.repository.core.index;
 
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.*;
import org.apache.lucene.document.NumberTools;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
 
/**
* Extended query parser supports Number to pad
* @author <a href="http://hasan.we4tech.com">nhm tanveer...(hasan)</a>
*/
public class ExtendedQueryParser extends QueryParser {
 
  private final Logger mLog = LogManager.getLogger(getClass());
  private TermValueEmbedFunctionExecutor mEmbedFunctionExecutor;
 
 
  public ExtendedQueryParser(final String pField,
                             final Analyzer pAnalyzer,
                             final TermValueEmbedFunctionExecutor pExecutor) {
    super(pField, pAnalyzer);
    mEmbedFunctionExecutor = pExecutor;
  }
 
  @Override
  protected Query getFuzzyQuery(final String pField,
                                final String pTermString,
                                final float pMinimumSimilarity)
      throws ParseException {
    return super.getFuzzyQuery(
        pField, executeEmbedFunction(pTermString), pMinimumSimilarity);
  }
 
  private String executeEmbedFunction(final String pTermString) {
    if (mEmbedFunctionExecutor == null) {
      return pTermString;
    } else {
      final String evaledString = mEmbedFunctionExecutor.eval(pTermString);
      if (mLog.isDebugEnabled()) {
        mLog.debug("Eval'd value - " + evaledString);
      }
      return evaledString;
    }
  }
 
  @Override
  protected Query getRangeQuery(final String pField,
                                final String pLowerTerm,
                                final String pUpperTerm,
                                final boolean pInclusive)
      throws ParseException {
 
    final boolean includeLower = pLowerTerm != null && !"*".equals(pLowerTerm);
    final boolean includeUpper = pUpperTerm != null && !"*".equals(pUpperTerm);
    return new ConstantScoreRangeQuery(
        pField,
        includeLower ? NumberTools.longToString(Long.parseLong(pLowerTerm)) : "",
        includeUpper ? NumberTools.longToString(Long.parseLong(pUpperTerm)) : "*",
        includeLower, includeUpper) {
    };
  }
}