@Test 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("high blood", first.getMatchedWords()); assertEquals("high blood pressure", first.getQueryString()); assertEquals("hypertension", first.getTagName()); assertEquals(73, (int) first.getScore()); ExplainedQueryString second = iterator.next(); assertEquals("medication", second.getMatchedWords()); assertEquals("medication", second.getQueryString()); assertEquals("medication", second.getTagName()); assertEquals(100, (int) second.getScore()); }
public Set<ExplainedQueryString> findQueriesFromExplanation( Map<String, String> originalQueryInMap, Explanation explanation) { Set<ExplainedQueryString> matchedQueryStrings = new LinkedHashSet<>(); Set<String> matchedQueryTerms = explainServiceHelper.findMatchedWords(explanation); for (String matchedQueryTerm : matchedQueryTerms) { Map<String, Double> matchedQueryRule = explainServiceHelper.findMatchQueries(matchedQueryTerm, originalQueryInMap); if (matchedQueryRule.size() > 0) { Entry<String, Double> entry = matchedQueryRule.entrySet().stream() .max(Comparator.comparingDouble(Entry::getValue)) .orElseThrow( () -> new IllegalStateException( "matchedQueryRule.entrySet() shouldn't return an empty Set")); matchedQueryStrings.add( ExplainedQueryString.create( matchedQueryTerm, entry.getKey(), originalQueryInMap.get(entry.getKey()), entry.getValue())); } } return matchedQueryStrings; }
ElasticSearchExplainServiceImpl implements ElasticSearchExplainService { public Set<ExplainedQueryString> findQueriesFromExplanation( Map<String, String> originalQueryInMap, Explanation explanation) { Set<ExplainedQueryString> matchedQueryStrings = new LinkedHashSet<>(); Set<String> matchedQueryTerms = explainServiceHelper.findMatchedWords(explanation); for (String matchedQueryTerm : matchedQueryTerms) { Map<String, Double> matchedQueryRule = explainServiceHelper.findMatchQueries(matchedQueryTerm, originalQueryInMap); if (matchedQueryRule.size() > 0) { Entry<String, Double> entry = matchedQueryRule.entrySet().stream() .max(Comparator.comparingDouble(Entry::getValue)) .orElseThrow( () -> new IllegalStateException( "matchedQueryRule.entrySet() shouldn't return an empty Set")); matchedQueryStrings.add( ExplainedQueryString.create( matchedQueryTerm, entry.getKey(), originalQueryInMap.get(entry.getKey()), entry.getValue())); } } return matchedQueryStrings; } }
ElasticSearchExplainServiceImpl implements ElasticSearchExplainService { public Set<ExplainedQueryString> findQueriesFromExplanation( Map<String, String> originalQueryInMap, Explanation explanation) { Set<ExplainedQueryString> matchedQueryStrings = new LinkedHashSet<>(); Set<String> matchedQueryTerms = explainServiceHelper.findMatchedWords(explanation); for (String matchedQueryTerm : matchedQueryTerms) { Map<String, Double> matchedQueryRule = explainServiceHelper.findMatchQueries(matchedQueryTerm, originalQueryInMap); if (matchedQueryRule.size() > 0) { Entry<String, Double> entry = matchedQueryRule.entrySet().stream() .max(Comparator.comparingDouble(Entry::getValue)) .orElseThrow( () -> new IllegalStateException( "matchedQueryRule.entrySet() shouldn't return an empty Set")); matchedQueryStrings.add( ExplainedQueryString.create( matchedQueryTerm, entry.getKey(), originalQueryInMap.get(entry.getKey()), entry.getValue())); } } return matchedQueryStrings; } ElasticSearchExplainServiceImpl(
      ElasticsearchService elasticsearchService, ExplainServiceHelper explainServiceHelper); }
ElasticSearchExplainServiceImpl implements ElasticSearchExplainService { public Set<ExplainedQueryString> findQueriesFromExplanation( Map<String, String> originalQueryInMap, Explanation explanation) { Set<ExplainedQueryString> matchedQueryStrings = new LinkedHashSet<>(); Set<String> matchedQueryTerms = explainServiceHelper.findMatchedWords(explanation); for (String matchedQueryTerm : matchedQueryTerms) { Map<String, Double> matchedQueryRule = explainServiceHelper.findMatchQueries(matchedQueryTerm, originalQueryInMap); if (matchedQueryRule.size() > 0) { Entry<String, Double> entry = matchedQueryRule.entrySet().stream() .max(Comparator.comparingDouble(Entry::getValue)) .orElseThrow( () -> new IllegalStateException( "matchedQueryRule.entrySet() shouldn't return an empty Set")); matchedQueryStrings.add( ExplainedQueryString.create( matchedQueryTerm, entry.getKey(), originalQueryInMap.get(entry.getKey()), entry.getValue())); } } return matchedQueryStrings; } ElasticSearchExplainServiceImpl(
      ElasticsearchService elasticsearchService, ExplainServiceHelper explainServiceHelper); Explanation explain(Query<Entity> q, EntityType entityType, Object entityId); Set<ExplainedQueryString> findQueriesFromExplanation(
      Map<String, String> originalQueryInMap, Explanation explanation); }
ElasticSearchExplainServiceImpl implements ElasticSearchExplainService { public Set<ExplainedQueryString> findQueriesFromExplanation( Map<String, String> originalQueryInMap, Explanation explanation) { Set<ExplainedQueryString> matchedQueryStrings = new LinkedHashSet<>(); Set<String> matchedQueryTerms = explainServiceHelper.findMatchedWords(explanation); for (String matchedQueryTerm : matchedQueryTerms) { Map<String, Double> matchedQueryRule = explainServiceHelper.findMatchQueries(matchedQueryTerm, originalQueryInMap); if (matchedQueryRule.size() > 0) { Entry<String, Double> entry = matchedQueryRule.entrySet().stream() .max(Comparator.comparingDouble(Entry::getValue)) .orElseThrow( () -> new IllegalStateException( "matchedQueryRule.entrySet() shouldn't return an empty Set")); matchedQueryStrings.add( ExplainedQueryString.create( matchedQueryTerm, entry.getKey(), originalQueryInMap.get(entry.getKey()), entry.getValue())); } } return matchedQueryStrings; } ElasticSearchExplainServiceImpl(
      ElasticsearchService elasticsearchService, ExplainServiceHelper explainServiceHelper); Explanation explain(Query<Entity> q, EntityType entityType, Object entityId); Set<ExplainedQueryString> findQueriesFromExplanation(
      Map<String, String> originalQueryInMap, Explanation explanation);  }