use of org.apache.lucene.search.Explanation in project lucene-solr by apache.
the class CustomScoreProvider method customExplain.
/**
* Explain the custom score.
* Whenever overriding {@link #customScore(int, float, float[])},
* this method should also be overridden to provide the correct explanation
* for the part of the custom scoring.
*
* @param doc doc being explained.
* @param subQueryExpl explanation for the sub-query part.
* @param valSrcExpls explanation for the value source part.
* @return an explanation for the custom score
*/
public Explanation customExplain(int doc, Explanation subQueryExpl, Explanation[] valSrcExpls) throws IOException {
if (valSrcExpls.length == 1) {
return customExplain(doc, subQueryExpl, valSrcExpls[0]);
}
if (valSrcExpls.length == 0) {
return subQueryExpl;
}
float valSrcScore = 1;
for (Explanation valSrcExpl : valSrcExpls) {
valSrcScore *= valSrcExpl.getValue();
}
List<Explanation> subs = new ArrayList<>();
subs.add(subQueryExpl);
for (Explanation valSrcExpl : valSrcExpls) {
subs.add(valSrcExpl);
}
return Explanation.match(valSrcScore * subQueryExpl.getValue(), "custom score: product of:", subs);
}
use of org.apache.lucene.search.Explanation in project molgenis by molgenis.
the class SearchServiceIT method testSemanticSearch.
@Test
public void testSemanticSearch() {
List<Entity> attributes = createDynamic(6).collect(toList());
attributes.get(0).set(ATTR_STRING, "High chance of pulmonary disease");
attributes.get(1).set(ATTR_STRING, "And now for something completely different...");
attributes.get(2).set(ATTR_STRING, "Are you taking hypertensive medication?");
attributes.get(3).set(ATTR_STRING, "Have you ever had high blood pressure? (Repeat) (1)");
attributes.get(4).set(ATTR_STRING, "Do you suffer from Ocular hypertension?");
attributes.get(5).set(ATTR_STRING, "Do you have a vascular disorder?");
Entity ontology1 = attributes.get(0).getEntity(ATTR_CATEGORICAL);
Entity ontology2 = attributes.get(1).getEntity(ATTR_CATEGORICAL);
for (Entity term : attributes) {
term.set(ATTR_CATEGORICAL, ontology1);
}
attributes.get(5).set(ATTR_CATEGORICAL, ontology2);
searchService.index(entityTypeDynamic, attributes.stream());
searchService.refreshIndex();
List<String> queryTerms = asList("hypertension", "disorder vascular hypertensive", "increased pressure blood", "high pressure blood", "ocular^0.5 hypertension^0.5", "hypertension^0.25 idiopathic^0.25 pulmonary^0.25");
QueryRule finalDisMaxQuery = new QueryRule(queryTerms.stream().flatMap(term -> Stream.of(new QueryRule(ATTR_STRING, FUZZY_MATCH, term), new QueryRule(ATTR_SCRIPT, FUZZY_MATCH, term))).collect(toList()));
finalDisMaxQuery.setOperator(DIS_MAX);
List<String> attributeIds = asList("0", "1", "2", "3", "4", "5");
Query<Entity> query = new QueryImpl<>(asList(new QueryRule(ATTR_ID, IN, attributeIds), new QueryRule(AND), finalDisMaxQuery));
List<Object> matchingAttributeIDs = searchService.search(entityTypeDynamic, query).collect(toList());
assertEquals(matchingAttributeIDs.get(0), "3");
assertEquals(matchingAttributeIDs.get(1), "5");
assertFalse(matchingAttributeIDs.contains("1"));
List<Explanation> explanations = attributeIds.stream().map(id -> explainService.explain(query, entityTypeDynamic, id)).collect(toList());
List<Float> scores = explanations.stream().map(Explanation::getValue).collect(toList());
// FIXME these scores vary between runs
// assertEquals(scores, asList(0.3463153, 0, 0.7889965, 1.7814579, 0.76421005, 1.0707202));
Map<String, String> expandedQueryMap = new HashMap<>();
for (String term : asList("hypertens", "disord vascular hypertens", "increased pressur blood", "high pressur blood", "ocular hypertens", "hypertens idiopathic pulmonary")) {
expandedQueryMap.put(term, "hypertension");
}
List<Set<ExplainedQueryString>> explanationStrings = explanations.stream().map(explanation -> explainService.findQueriesFromExplanation(expandedQueryMap, explanation)).collect(toList());
List<Set<ExplainedQueryString>> expectedExplanationStrings = asList(// High chance of pulmonary disease
singleton(ExplainedQueryString.create("high", "high pressur blood", "hypertension", 41.66666666666667)), // And now for something completely different...
emptySet(), // Are you taking hypertensive medication?
singleton(ExplainedQueryString.create("hypertens", "hypertens", "hypertension", 100.0)), // Have you ever had high blood pressure? (Repeat) (1)
singleton(ExplainedQueryString.create("high pressur blood", "high pressur blood", "hypertension", 100.0)), // Do you suffer from Ocular hypertension?
singleton(ExplainedQueryString.create("ocular hypertens", "ocular hypertens", "hypertension", 100.0)), // Do you have a vascular disorder?
singleton(ExplainedQueryString.create("disord vascular", "disord vascular hypertens", "hypertension", 78.04878048780488)));
assertEquals(explanationStrings, expectedExplanationStrings);
}
use of org.apache.lucene.search.Explanation in project molgenis by molgenis.
the class ElasticSearchExplainServiceImplTest method testDiscoverMatchedQueries.
@Test
public void testDiscoverMatchedQueries() {
Explanation explanation_2 = Explanation.match(Float.valueOf("3.6267629"), "sum of:", Explanation.match(Float.valueOf("2.0587344"), "weight(label:high in 328) [PerFieldSimilarity], result of:"), Explanation.match(Float.valueOf("1.5680285"), "weight(label:blood in 328) [PerFieldSimilarity], result of:"));
Explanation explanation_3 = Explanation.match(Float.valueOf("1.754909"), "max of:", Explanation.match(Float.valueOf("1.754909"), "weight(label:medication in 328) [PerFieldSimilarity], result of:"));
Explanation explanation_1 = Explanation.match(Float.valueOf("5.381672"), "sum of:", explanation_2, explanation_3);
Set<String> actual = explainServiceHelper.findMatchedWords(explanation_1);
assertEquals(actual.size(), 2);
assertTrue(actual.contains("high blood"));
assertTrue(actual.contains("medication"));
}
use of org.apache.lucene.search.Explanation in project molgenis by molgenis.
the class SemanticSearchServiceImpl method isSingleMatchHighQuality.
boolean isSingleMatchHighQuality(Collection<String> queryTerms, Collection<String> ontologyTermQueries, Iterable<ExplainedQueryString> explanations) {
Map<String, Double> matchedTags = new HashMap<>();
for (ExplainedQueryString explanation : explanations) {
matchedTags.put(explanation.getTagName().toLowerCase(), explanation.getScore());
}
ontologyTermQueries.removeAll(queryTerms);
if (!queryTerms.isEmpty() && queryTerms.stream().anyMatch(token -> isGoodMatch(matchedTags, token)))
return true;
if (!ontologyTermQueries.isEmpty() && ontologyTermQueries.stream().allMatch(token -> isGoodMatch(matchedTags, token)))
return true;
return false;
}
use of org.apache.lucene.search.Explanation in project molgenis by molgenis.
the class ElasticSearchExplainServiceImplTest method testReverseSearchQueryStrings.
@Test
public void testReverseSearchQueryStrings() {
Explanation explanation_2 = Explanation.match(Float.valueOf("3.6267629"), "sum of:", Explanation.match(Float.valueOf("2.0587344"), "weight(label:high in 328) [PerFieldSimilarity], result of:"), Explanation.match(Float.valueOf("1.5680285"), "weight(label:blood in 328) [PerFieldSimilarity], result of:"));
Explanation explanation_3 = Explanation.match(Float.valueOf("1.754909"), "max of:", Explanation.match(Float.valueOf("1.754909"), "weight(label:medication in 328) [PerFieldSimilarity], result of:"));
Explanation explanation_1 = Explanation.match(Float.valueOf("5.381672"), "sum of:", explanation_2, explanation_3);
Map<String, String> expanedQueryMap = new HashMap<>();
expanedQueryMap.put("hypertension", "hypertension");
expanedQueryMap.put("hypertensive disorder", "hypertension");
expanedQueryMap.put("high blood pressure", "hypertension");
expanedQueryMap.put("medication", "medication");
expanedQueryMap.put("drug", "medication");
expanedQueryMap.put("pill", "medication");
Set<ExplainedQueryString> reverseSearchQueryStrings = elasticSearchExplainService.findQueriesFromExplanation(expanedQueryMap, explanation_1);
Iterator<ExplainedQueryString> iterator = reverseSearchQueryStrings.iterator();
ExplainedQueryString first = iterator.next();
assertEquals(first.getMatchedWords(), "high blood");
assertEquals(first.getQueryString(), "high blood pressure");
assertEquals(first.getTagName(), "hypertension");
assertEquals((int) first.getScore(), 73);
ExplainedQueryString second = iterator.next();
assertEquals(second.getMatchedWords(), "medication");
assertEquals(second.getQueryString(), "medication");
assertEquals(second.getTagName(), "medication");
assertEquals((int) second.getScore(), 100);
}
Aggregations