Search in sources :

Example 6 with ExplainedQueryString

use of org.molgenis.semanticsearch.explain.bean.ExplainedQueryString 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;
}
Also used : NGramDistanceAlgorithm(org.molgenis.semanticsearch.string.NGramDistanceAlgorithm) java.util(java.util) StringDistance(org.apache.lucene.search.spell.StringDistance) Operator(org.molgenis.data.QueryRule.Operator) LoggerFactory(org.slf4j.LoggerFactory) SemanticSearchService(org.molgenis.semanticsearch.service.SemanticSearchService) QueryImpl(org.molgenis.data.support.QueryImpl) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) MetaDataService(org.molgenis.data.meta.MetaDataService) Lists(com.google.common.collect.Lists) FluentIterable(com.google.common.collect.FluentIterable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Objects.requireNonNull(java.util.Objects.requireNonNull) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) OntologyService(org.molgenis.ontology.core.service.OntologyService) Splitter(com.google.common.base.Splitter) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) Hit(org.molgenis.semanticsearch.semantic.Hit) Stemmer(org.molgenis.semanticsearch.string.Stemmer) Explanation(org.apache.lucene.search.Explanation) Logger(org.slf4j.Logger) ElasticSearchExplainService(org.molgenis.semanticsearch.explain.service.ElasticSearchExplainService) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Ontology(org.molgenis.ontology.core.model.Ontology) Stream(java.util.stream.Stream) Ordering(com.google.common.collect.Ordering) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) DataService(org.molgenis.data.DataService) Query(org.molgenis.data.Query) QueryRule(org.molgenis.data.QueryRule) Joiner(com.google.common.base.Joiner) Entity(org.molgenis.data.Entity) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)

Example 7 with ExplainedQueryString

use of org.molgenis.semanticsearch.explain.bean.ExplainedQueryString 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);
}
Also used : HashMap(java.util.HashMap) Explanation(org.apache.lucene.search.Explanation) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) Test(org.testng.annotations.Test)

Aggregations

ExplainedQueryString (org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)7 Explanation (org.apache.lucene.search.Explanation)4 EntityType (org.molgenis.data.meta.model.EntityType)4 Test (org.testng.annotations.Test)3 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Stream (java.util.stream.Stream)2 Entity (org.molgenis.data.Entity)2 QueryRule (org.molgenis.data.QueryRule)2 Operator (org.molgenis.data.QueryRule.Operator)2 Attribute (org.molgenis.data.meta.model.Attribute)2 QueryImpl (org.molgenis.data.support.QueryImpl)2 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)2 ElasticSearchExplainService (org.molgenis.semanticsearch.explain.service.ElasticSearchExplainService)2 Joiner (com.google.common.base.Joiner)1 Splitter (com.google.common.base.Splitter)1 FluentIterable (com.google.common.collect.FluentIterable)1 Lists (com.google.common.collect.Lists)1 Ordering (com.google.common.collect.Ordering)1 Sets (com.google.common.collect.Sets)1