All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.molgenis.semanticsearch.explain.service.ElasticSearchExplainServiceImpl Maven / Gradle / Ivy

The newest version!
package org.molgenis.semanticsearch.explain.service;

import static java.util.Objects.requireNonNull;

import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.lucene.search.Explanation;
import org.molgenis.data.Entity;
import org.molgenis.data.Query;
import org.molgenis.data.elasticsearch.ElasticsearchService;
import org.molgenis.data.meta.model.EntityType;
import org.molgenis.semanticsearch.explain.bean.ExplainedQueryString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ElasticSearchExplainServiceImpl implements ElasticSearchExplainService {
  private static final Logger LOG = LoggerFactory.getLogger(ElasticSearchExplainServiceImpl.class);

  private final ElasticsearchService elasticsearchService;
  private final ExplainServiceHelper explainServiceHelper;

  public ElasticSearchExplainServiceImpl(
      ElasticsearchService elasticsearchService, ExplainServiceHelper explainServiceHelper) {
    this.elasticsearchService = requireNonNull(elasticsearchService);
    this.explainServiceHelper = requireNonNull(explainServiceHelper);
  }

  public Explanation explain(Query q, EntityType entityType, Object entityId) {
    Explanation explanation = elasticsearchService.explain(entityType, entityId, q);
    if (explanation != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(explanation.toString());
      }

      return explanation;
    }
    return null;
  }

  public Set findQueriesFromExplanation(
      Map originalQueryInMap, Explanation explanation) {
    Set matchedQueryStrings = new LinkedHashSet<>();
    Set matchedQueryTerms = explainServiceHelper.findMatchedWords(explanation);
    for (String matchedQueryTerm : matchedQueryTerms) {
      Map matchedQueryRule =
          explainServiceHelper.findMatchQueries(matchedQueryTerm, originalQueryInMap);

      if (matchedQueryRule.size() > 0) {
        Entry 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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy