Java Code Examples for org.apache.lucene.search.Explanation#getDetails()

The following examples show how to use org.apache.lucene.search.Explanation#getDetails() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExplainAugmenterFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Render an explanation as HTML. */
public static String toHtml(Explanation explanation) {
  StringBuilder buffer = new StringBuilder();
  buffer.append("<ul>\n");

  buffer.append("<li>");
  buffer.append(explanation.getValue()).append(" = ").append(explanation.getDescription());
  buffer.append("<br />\n");

  Explanation[] details = explanation.getDetails();
  for (int i = 0 ; i < details.length; i++) {
    buffer.append(toHtml(details[i]));
  }

  buffer.append("</li>\n");
  buffer.append("</ul>\n");

  return buffer.toString();
}
 
Example 2
Source File: SolrPluginUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static NamedList<Object> explanationToNamedList(Explanation e) {
  NamedList<Object> out = new SimpleOrderedMap<>();

  out.add("match", e.isMatch());
  out.add("value", e.getValue());
  out.add("description", e.getDescription());

  Explanation[] details = e.getDetails();

  // short circut out
  if (0 == details.length) return out;

  List<NamedList<Object>> kids
    = new ArrayList<>(details.length);
  for (Explanation d : details) {
    kids.add(explanationToNamedList(d));
  }
  out.add("details", kids);

  return out;
}
 
Example 3
Source File: LtrQueryTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void checkFeatureNames(Explanation expl, List<PrebuiltFeature> features) {
    Explanation[] expls = expl.getDetails();
    int ftrIdx = 0;
    for (Explanation ftrExpl: expls) {
        String ftrName = features.get(ftrIdx).name();
        String expectedFtrName;
        if (ftrName == null) {
            expectedFtrName = "Feature " + ftrIdx + ":";
        } else {
            expectedFtrName = "Feature " + ftrIdx + "(" + ftrName + "):";
        }

        String ftrExplainStart = ftrExpl.getDescription().substring(0,expectedFtrName.length());
        assertEquals(expectedFtrName, ftrExplainStart);

        ftrIdx++;
    }
}
 
Example 4
Source File: InternalSearchHit.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void buildExplanation(XContentBuilder builder, Explanation explanation) throws IOException {
    builder.startObject();
    builder.field(Fields.VALUE, explanation.getValue());
    builder.field(Fields.DESCRIPTION, explanation.getDescription());
    Explanation[] innerExps = explanation.getDetails();
    if (innerExps != null) {
        builder.startArray(Fields.DETAILS);
        for (Explanation exp : innerExps) {
            buildExplanation(builder, exp);
        }
        builder.endArray();
    }
    builder.endObject();
}
 
Example 5
Source File: LindenResultParser.java    From linden with Apache License 2.0 5 votes vote down vote up
private LindenExplanation parseLindenExplanation(Explanation expl) {
  LindenExplanation lindenExpl = new LindenExplanation();
  lindenExpl.setDescription(expl.getDescription());
  lindenExpl.setValue(expl.getValue());
  if (expl.getDetails() != null) {
    for (Explanation subExpl : expl.getDetails()) {
      LindenExplanation subLindenExpl = parseLindenExplanation(subExpl);
      lindenExpl.addToDetails(subLindenExpl);
    }
  }
  return lindenExpl;
}
 
Example 6
Source File: SweetSpotSimilarityTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Explanation findExplanation(Explanation expl, String text) {
  if (expl.getDescription().startsWith(text)) {
    return expl;
  } else {
    for (Explanation sub : expl.getDetails()) {
      Explanation match = findExplanation(sub, text);
      if (match != null) {
        return match;
      }
    }
  }
  return null;
}
 
Example 7
Source File: TestSweetSpotSimilarityFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Explanation findExplanation(Explanation expl, String text) {
  if (expl.getDescription().startsWith(text)) {
    return expl;
  } else {
    for (Explanation sub : expl.getDetails()) {
      Explanation match = findExplanation(sub, text);
      if (match != null) {
        return match;
      }
    }
  }
  return null;
}
 
Example 8
Source File: ExplainServiceHelper.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * This method is able to recursively collect all the matched words from Elasticsearch Explanation
 * document
 *
 * @return a set of matched words that are matched to different ontology terms
 */
Set<String> findMatchedWords(Explanation explanation) {
  Set<String> words = new HashSet<>();
  String description = explanation.getDescription();
  if (description.startsWith(Options.SUM_OF.toString())
      || description.startsWith(Options.PRODUCT_OF.toString())) {
    if (newArrayList(explanation.getDetails()).stream().allMatch(this::reachLastLevel)) {
      words.add(extractMatchedWords(explanation.getDetails()));
    } else {
      for (Explanation subExplanation : explanation.getDetails()) {
        words.addAll(findMatchedWords(subExplanation));
      }
    }
  } else if (description.startsWith(Options.MAX_OF.toString())) {
    Explanation maxExplanation =
        newArrayList(explanation.getDetails()).stream()
            .max(
                (explanation1, explanation2) ->
                    Float.compare(explanation1.getValue(), explanation2.getValue()))
            .orElseThrow(
                () ->
                    new IllegalStateException(
                        "explanation.getDetails() shouldn't return an empty array"));
    words.addAll(findMatchedWords(maxExplanation));
  } else if (description.startsWith(Options.WEIGHT.toString())) {
    words.add(getMatchedWord(description));
  }
  return words;
}
 
Example 9
Source File: ExplainRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
private void buildExplanation(XContentBuilder builder, Explanation explanation) throws IOException {
    builder.field(Fields.VALUE, explanation.getValue());
    builder.field(Fields.DESCRIPTION, explanation.getDescription());
    Explanation[] innerExps = explanation.getDetails();
    if (innerExps != null) {
        builder.startArray(Fields.DETAILS);
        for (Explanation exp : innerExps) {
            builder.startObject();
            buildExplanation(builder, exp);
            builder.endObject();
        }
        builder.endArray();
    }
}