Search in sources :

Example 1 with ATTR_SCRIPT

use of org.molgenis.data.EntityTestHarness.ATTR_SCRIPT in project molgenis by molgenis.

the class SearchServiceIT method testSemanticSearch.

@Test
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("3", matchingAttributeIDs.get(0));
    assertEquals("5", matchingAttributeIDs.get(1));
    assertFalse(matchingAttributeIDs.contains("1"));
    List<Explanation> explanations = attributeIds.stream().map(id -> explainService.explain(query, entityTypeDynamic, id)).collect(toList());
    // FIXME these scores vary between runs
    // List<Float> scores = explanations.stream().map(Explanation::getValue).collect(toList());
    // 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(expectedExplanationStrings, explanationStrings);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ATTR_BOOL(org.molgenis.data.EntityTestHarness.ATTR_BOOL) ATTR_CATEGORICAL_MREF(org.molgenis.data.EntityTestHarness.ATTR_CATEGORICAL_MREF) IN(org.molgenis.data.QueryRule.Operator.IN) ATTR_INT(org.molgenis.data.EntityTestHarness.ATTR_INT) MolgenisDateFormat.parseLocalDate(org.molgenis.data.util.MolgenisDateFormat.parseLocalDate) ATTR_HYPERLINK(org.molgenis.data.EntityTestHarness.ATTR_HYPERLINK) Autowired(org.springframework.beans.factory.annotation.Autowired) Collections.singletonList(java.util.Collections.singletonList) ATTR_SCRIPT(org.molgenis.data.EntityTestHarness.ATTR_SCRIPT) Collections.singleton(java.util.Collections.singleton) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Arrays.asList(java.util.Arrays.asList) Sort(org.molgenis.data.Sort) Map(java.util.Map) ElasticsearchService(org.molgenis.data.elasticsearch.ElasticsearchService) ATTR_EMAIL(org.molgenis.data.EntityTestHarness.ATTR_EMAIL) MethodSource(org.junit.jupiter.params.provider.MethodSource) EntityTestHarness(org.molgenis.data.EntityTestHarness) Explanation(org.apache.lucene.search.Explanation) Collections.emptyList(java.util.Collections.emptyList) MolgenisDateFormat.parseInstant(org.molgenis.data.util.MolgenisDateFormat.parseInstant) Set(java.util.Set) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) EntityType(org.molgenis.data.meta.model.EntityType) AND(org.molgenis.data.QueryRule.Operator.AND) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) FUZZY_MATCH(org.molgenis.data.QueryRule.Operator.FUZZY_MATCH) Query(org.molgenis.data.Query) QueryRule(org.molgenis.data.QueryRule) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ATTR_DATETIME(org.molgenis.data.EntityTestHarness.ATTR_DATETIME) ATTR_HTML(org.molgenis.data.EntityTestHarness.ATTR_HTML) ATTR_DATE(org.molgenis.data.EntityTestHarness.ATTR_DATE) HashMap(java.util.HashMap) QueryImpl(org.molgenis.data.support.QueryImpl) Supplier(java.util.function.Supplier) ATTR_CATEGORICAL(org.molgenis.data.EntityTestHarness.ATTR_CATEGORICAL) ATTR_STRING(org.molgenis.data.EntityTestHarness.ATTR_STRING) ATTR_ID(org.molgenis.data.EntityTestHarness.ATTR_ID) ATTR_REF_STRING(org.molgenis.data.EntityTestHarness.ATTR_REF_STRING) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ATTR_XREF(org.molgenis.data.EntityTestHarness.ATTR_XREF) Collections.emptySet(java.util.Collections.emptySet) ATTR_MREF(org.molgenis.data.EntityTestHarness.ATTR_MREF) ElasticSearchExplainService(org.molgenis.semanticsearch.explain.service.ElasticSearchExplainService) DIS_MAX(org.molgenis.data.QueryRule.Operator.DIS_MAX) ATTR_DECIMAL(org.molgenis.data.EntityTestHarness.ATTR_DECIMAL) Collectors.toList(java.util.stream.Collectors.toList) ATTR_LONG(org.molgenis.data.EntityTestHarness.ATTR_LONG) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) AbstractMockitoSpringContextTests(org.molgenis.test.AbstractMockitoSpringContextTests) ContextConfiguration(org.springframework.test.context.ContextConfiguration) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) Entity(org.molgenis.data.Entity) Entity(org.molgenis.data.Entity) Set(java.util.Set) Collections.emptySet(java.util.Collections.emptySet) HashMap(java.util.HashMap) Explanation(org.apache.lucene.search.Explanation) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) QueryImpl(org.molgenis.data.support.QueryImpl) QueryRule(org.molgenis.data.QueryRule) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptySet (java.util.Collections.emptySet)1 Collections.singleton (java.util.Collections.singleton)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Supplier (java.util.function.Supplier)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Explanation (org.apache.lucene.search.Explanation)1 AfterEach (org.junit.jupiter.api.AfterEach)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1