Example usage for org.apache.lucene.search Explanation getDescription

List of usage examples for org.apache.lucene.search Explanation getDescription

Introduction

In this page you can find the example usage for org.apache.lucene.search Explanation getDescription.

Prototype

public String getDescription() 

Source Link

Document

A description of this explanation node.

Usage

From source file:com.doculibre.constellio.lucene.LuceneSearchResultsProvider.java

License:Open Source License

private List<String> getMatchingFieldNames(int docId, Explanation explanation) {
    Set<String> matchingFieldNames = new HashSet<String>();
    if (explanation instanceof ComplexExplanation) {
        Explanation[] details = explanation.getDetails();
        if (details != null) {
            for (int i = 0; i < details.length; i++) {
                Explanation detail = details[i];
                // Recursive call
                matchingFieldNames.addAll(getMatchingFieldNames(docId, detail));
            }// www .jav a 2 s. c om
        }
    }

    String description = explanation.getDescription();
    int indexOfFirstChar = 0;
    int indexOfColon = description.indexOf(":");
    while (indexOfColon != -1) {
        String delim = "(";
        int indexOfDelim = description.indexOf(delim, indexOfFirstChar);
        if (indexOfDelim != -1) {
            int indexOfFieldName = indexOfDelim + delim.length();
            String matchingFieldName = description.substring(indexOfFieldName, indexOfColon);
            if (matchingFieldName.startsWith("termFreq(")) {
                matchingFieldName = matchingFieldName.substring("termFreq(".length());
            }
            matchingFieldNames.add(matchingFieldName);
            indexOfFirstChar = indexOfColon + 1;
            indexOfColon = description.indexOf(":", indexOfFirstChar);
        } else {
            break;
        }
    }
    return new ArrayList<String>(matchingFieldNames);
}

From source file:com.o19s.es.ltr.query.LtrQueryTests.java

License:Apache License

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;//from  w w  w .  ja v a  2 s  . c  o  m
        if (ftrName == null) {
            expectedFtrName = "Feature " + ftrIdx + ":";
        } else {
            expectedFtrName = "Feature " + ftrIdx + "(" + ftrName + "):";
        }

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

        ftrIdx++;
    }
}

From source file:com.senseidb.search.req.SenseiRequestProtoSerializer.java

License:Apache License

private SenseiProtos.Explanation convertExplanation(Explanation explanation) {
    SenseiProtos.Explanation.Builder builder = SenseiProtos.Explanation.newBuilder();

    builder.setDescription(explanation.getDescription());
    builder.setValue(explanation.getValue());
    if (explanation.getDetails() != null && explanation.getDetails().length > 0) {
        for (int i = 0; i < explanation.getDetails().length; i++) {
            builder.addDetails(convertExplanation(explanation.getDetails()[i]));
        }/*ww  w .  j  ava 2  s  .  c o m*/
    }

    return builder.build();
}

From source file:com.xiaomi.linden.core.search.LindenResultParser.java

License:Apache License

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);
        }//  w  w  w.  j  a v a 2s  . c  o  m
    }
    return lindenExpl;
}

From source file:org.apache.solr.util.SolrPluginUtils.java

License:Apache License

public static NamedList<Object> explanationToNamedList(Explanation e) {
    NamedList<Object> out = new SimpleOrderedMap<Object>();

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

    Explanation[] details = e.getDetails();

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

    List<NamedList<Object>> kids = new ArrayList<NamedList<Object>>(details.length);
    for (Explanation d : details) {
        kids.add(explanationToNamedList(d));
    }/*from   w w w . j  ava2s.  c  om*/
    out.add("details", kids);

    return out;
}

From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java

License:Apache License

public static void writeExplanation(StreamOutput out, Explanation explanation) throws IOException {
    out.writeBoolean(explanation.isMatch());
    out.writeString(explanation.getDescription());
    Explanation[] subExplanations = explanation.getDetails();
    out.writeVInt(subExplanations.length);
    for (Explanation subExp : subExplanations) {
        writeExplanation(out, subExp);/*from ww  w.j  av  a2s . com*/
    }
    if (explanation.isMatch()) {
        out.writeFloat(explanation.getValue());
    }
}

From source file:org.compass.core.test.find.FindTests.java

License:Apache License

public void testLuceneExplanation() {
    addDataA(0, 10);//from w  w w.  j  a v  a  2  s . com

    CompassSession session = openSession();
    CompassTransaction tr = session.beginTransaction();
    CompassHits hits = session.find("alias:a1 OR alias:b1");
    assertEquals(10, hits.getLength());

    Explanation explanation = LuceneHelper.getLuceneSearchEngineHits(hits).explain(0);
    assertNotNull(explanation);
    assertEquals("product of:", explanation.getDescription());

    tr.commit();
    session.close();
}

From source file:org.elasticsearch.common.lucene.client.Lucene.java

License:Apache License

public static void writeExplanation(StreamOutput out, Explanation explanation) throws IOException {
    out.writeFloat(explanation.getValue());
    out.writeUTF(explanation.getDescription());
    Explanation[] subExplanations = explanation.getDetails();
    if (subExplanations == null) {
        out.writeBoolean(false);/*  w w  w .  jav  a 2 s . c o  m*/
    } else {
        out.writeBoolean(true);
        out.writeVInt(subExplanations.length);
        for (Explanation subExp : subExplanations) {
            writeExplanation(out, subExp);
        }
    }
}

From source file:org.elasticsearch.common.lucene.Lucene.java

License:Apache License

public static void writeExplanation(StreamOutput out, Explanation explanation) throws IOException {
    out.writeFloat(explanation.getValue());
    out.writeString(explanation.getDescription());
    Explanation[] subExplanations = explanation.getDetails();
    if (subExplanations == null) {
        out.writeBoolean(false);/*from   w ww  . jav  a2s .c  o  m*/
    } else {
        out.writeBoolean(true);
        out.writeVInt(subExplanations.length);
        for (Explanation subExp : subExplanations) {
            writeExplanation(out, subExp);
        }
    }
}

From source file:org.elasticsearch.common.lucene.search.function.RandomScoreFunctionTests.java

License:Apache License

@Test
public void testExplainScoreReportsOriginalSeed() {
    long seed = randomLong();
    Explanation subExplanation = new Explanation();

    RandomScoreFunction function = new RandomScoreFunction(seed);
    // Trigger a random call to change the seed to ensure that we are
    //  reporting the _original_ seed
    function.score(0, 1.0f);//ww  w  .j  av  a 2s.c  om

    // Generate the randomScore explanation
    Explanation randomExplanation = function.explainScore(0, subExplanation);

    // Original seed should be there
    assertThat(randomExplanation.getDescription(), containsString("" + seed));
    assertThat(randomExplanation.getDetails(), arrayContaining(subExplanation));
}