Ejemplo n.º 1
0
  public void testBuildSnippetNoPlaceHolder() {
    final String snippet = "something.getAnother().equals(blah);";
    final SnippetBuilder snip = new SnippetBuilder(snippet);
    final String cellValue = "this is ignored...";
    final String result = snip.build(cellValue);

    assertEquals(snippet, result);
  }
Ejemplo n.º 2
0
  public void testSingleParamMultipleTimes() {
    final String snippet = "something.param.getAnother($param).equals($param);";
    final SnippetBuilder snip = new SnippetBuilder(snippet);
    final String cellValue = "42";
    final String result = snip.build(cellValue);
    assertNotNull(result);

    assertEquals("something.param.getAnother(42).equals(42);", result);
  }
Ejemplo n.º 3
0
  public void testBuildSnippet() {
    final String snippet = "something.param.getAnother().equals($param);";
    final SnippetBuilder snip = new SnippetBuilder(snippet);
    final String cellValue = "$42";
    final String result = snip.build(cellValue);
    assertNotNull(result);

    assertEquals("something.param.getAnother().equals($42);", result);
  }
Ejemplo n.º 4
0
 public void testForAllOrAndMultipleWithPrefix() {
   final String snippet =
       "something == this && forall(||){something == $} && forall(&&){something < $}";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result = snip.build("x, y");
   assertEquals(
       "something == this && something == x || something == y && something < x && something < y",
       result);
 }
Ejemplo n.º 5
0
 public void testMultiPlaceHolderEscapedComma() {
   final String snippet = "rulesOutputRouting.set( $1, $2, $3, $4, $5 );";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result =
       snip.build(
           "\"80\",\"Department Manager\",toa.getPersonExpense().getEntityCode(\"Part Of\"\\,\"Office\"),10004,30");
   assertEquals(
       "rulesOutputRouting.set( \"80\", \"Department Manager\", toa.getPersonExpense().getEntityCode(\"Part Of\",\"Office\"), 10004, 30 );",
       result);
 }
Ejemplo n.º 6
0
 public void testForAllAndNone() {
   final String snippet = "forall(&&){something == $}";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result = snip.build("");
   assertEquals("forall(&&){something == $}", result);
 }
Ejemplo n.º 7
0
 public void testStartWithParam() {
   final String snippet = "$1 goo $2";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result = snip.build("x, y");
   assertEquals("x goo y", result);
 }
Ejemplo n.º 8
0
 public void testMultiPlaceHolderSingle() {
   final String snippet = "something.getAnother($1).equals($1);";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result = snip.build("x");
   assertEquals("something.getAnother(x).equals(x);", result);
 }
Ejemplo n.º 9
0
 public void testMultiPlaceHolder() {
   final String snippet = "something.getAnother($1,$2).equals($2, '$2');";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result = snip.build("x, y");
   assertEquals("something.getAnother(x,y).equals(y, 'y');", result);
 }
Ejemplo n.º 10
0
 public void testForAllOr() {
   final String snippet = "forall(||){something == $}";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result = snip.build("x");
   assertEquals("something == x", result);
 }
Ejemplo n.º 11
0
 public void testForAllAndCSVMultiple() {
   final String snippet = "forall(&&){something == $ || something == $}";
   final SnippetBuilder snip = new SnippetBuilder(snippet);
   final String result = snip.build("x, y");
   assertEquals("something == x || something == x && something == y || something == y", result);
 }
Ejemplo n.º 12
0
  public boolean getSnippets(
      final int docId,
      final ReaderInterface reader,
      final List<FieldValueItem> values,
      final List<FieldValueItem> snippets,
      final Timer parentTimer)
      throws IOException, ParseException, SyntaxError, SearchLibException {

    if (values == null) return false;

    final Timer timer = new Timer(parentTimer, "SnippetField " + this.name);
    final long halfTimeExpiration =
        this.timeLimit == 0 ? 0 : timer.getStartOffset(this.timeLimit / 2);
    final long expiration = this.timeLimit == 0 ? 0 : timer.getStartOffset(this.timeLimit);

    FragmenterAbstract fragmenter = fragmenterTemplate.newInstance();
    SnippetVector currentVector = null;

    Timer t = new Timer(timer, "extractTermVectorIterator");

    Iterator<SnippetVector> vectorIterator =
        SnippetVectors.extractTermVectorIterator(
            docId, reader, snippetQueries, name, values, indexAnalyzer, t, halfTimeExpiration);
    if (vectorIterator != null)
      currentVector = vectorIterator.hasNext() ? vectorIterator.next() : null;

    t.end(null);

    t = new Timer(timer, "getFraments");

    int startOffset = 0;
    FragmentList fragments = new FragmentList();
    int vectorOffset = 0;
    for (FieldValueItem valueItem : values) {
      String value = valueItem.getValue();
      if (value != null) {
        // VectorOffset++ depends of EndOffset bug #patch Lucene 579 and
        // 1458
        fragmenter.getFragments(value, fragments, vectorOffset++);
      }
    }

    t.end(null);

    if (fragments.size() == 0) {
      timer.end(null);
      return false;
    }

    t = new Timer(timer, "checkValue");

    Fragment fragment = fragments.first();
    while (fragment != null) {
      currentVector = checkValue(currentVector, vectorIterator, startOffset, fragment);
      startOffset += fragment.getOriginalText().length();
      fragment = fragment.next();
    }

    t.end(null);

    Timer sbTimer = new Timer(timer, "snippetBuilder");

    boolean result = false;
    int snippetCounter = maxSnippetNumber;
    int scoredFragment = 0;
    while (snippetCounter-- != 0) {
      Fragment bestScoreFragment = null;
      fragment = Fragment.findNextHighlightedFragment(fragments.first());
      List<Fragment> scoreFragments = new ArrayList<Fragment>(0);
      double maxSearchScore = 0;

      t = new Timer(sbTimer, "fragmentScore");
      boolean expired = false;

      while (fragment != null) {
        double sc = fragment.searchScore(name, queryAnalyzer, query);
        if (sc > maxSearchScore) maxSearchScore = sc;
        scoreFragments.add(fragment);
        fragment = Fragment.findNextHighlightedFragment(fragment.next());
        scoredFragment++;
        if (expiration != 0) {
          if (System.currentTimeMillis() > expiration) {
            expired = true;
            break;
          }
        }
      }

      t.end("fragmentScore " + scoredFragment + " " + expired);

      for (Fragment frag : scoreFragments)
        bestScoreFragment =
            Fragment.bestScore(bestScoreFragment, frag, maxSearchScore, maxSnippetSize);

      if (bestScoreFragment != null) {
        SnippetBuilder snippetBuilder =
            new SnippetBuilder(maxSnippetSize, unescapedSeparator, tags, bestScoreFragment);
        if (snippetBuilder.length() > 0)
          snippets.add(new FieldValueItem(FieldValueOriginEnum.SNIPPET, snippetBuilder.toString()));
        fragments.remove(snippetBuilder.getFragments());
        result = true;
        continue;
      }

      if (fragments.first() == null) break;
      SnippetBuilder snippetBuilder =
          new SnippetBuilder(maxSnippetSize, unescapedSeparator, tags, fragments.first());
      if (snippetBuilder.length() > 0) {
        snippets.add(new FieldValueItem(FieldValueOriginEnum.SNIPPET, snippetBuilder.toString()));
        fragments.remove(snippetBuilder.getFragments());
      }
    }

    sbTimer.end(null);

    timer.end(null);

    return result;
  }