Search in sources :

Example 1 with SolrMatcherQueryExecutor

use of won.matcher.solr.query.SolrMatcherQueryExecutor in project webofneeds by researchstudio-sat.

the class SolrMatcherActor method processActiveNeedEvent.

protected void processActiveNeedEvent(NeedEvent needEvent) throws IOException, SolrServerException, JsonLdError {
    log.info("Start processing active need event {}", needEvent);
    // check if the need has doNotMatch flag, then do not use it for querying or indexing
    Dataset dataset = needEvent.deserializeNeedDataset();
    NeedModelWrapper needModelWrapper = new NeedModelWrapper(dataset);
    if (needModelWrapper.hasFlag(WON.NO_HINT_FOR_ME) && needModelWrapper.hasFlag(WON.NO_HINT_FOR_COUNTERPART)) {
        log.info("Discarding received need due to flags won:NoHintForMe and won:NoHintForCounterpart: {}", needEvent);
        return;
    }
    // check if need is usedForTesting only
    boolean usedForTesting = needModelWrapper.hasFlag(WON.USED_FOR_TESTING);
    SolrMatcherQueryExecutor queryExecutor = (usedForTesting ? testQueryExecuter : defaultQueryExecuter);
    // create another query depending if the current need is "WhatsAround" or a default need
    String queryString = null;
    if (needModelWrapper.hasFlag(WON.WHATS_AROUND)) {
        // WhatsAround doesnt match on terms only other needs in close location are boosted
        WhatsAroundQueryFactory qf = new WhatsAroundQueryFactory(dataset);
        queryString = qf.createQuery();
    } else {
        // default query matches content terms (of fields title, description and tags) with different weights
        // and gives an additional multiplicative boost for geographically closer needs
        DefaultNeedQueryFactory qf = new DefaultNeedQueryFactory(dataset);
        queryString = qf.createQuery();
    }
    // add filters to the query: default filters are
    // - need status active
    // - creation date overlap 1 month
    // - OR-filtering for matching contexts if any were specified
    // now create three slightly different queries for different lists of needs:
    // 1) needs without NoHintForCounterpart => hints for current need
    // 2) needs without NoHintForSelf, excluding WhatsAround needs => hints for needs in index that are not WhatsAround
    // 3) needs without NoHintForSelf that are only WhatsAround needs => hints for needs in index that are WhatsAround
    // to achieve this use a different filters for these queries
    // case 1) needs without NoHintForCounterpart => hints for current need
    List<String> filterQueries = new LinkedList<>();
    filterQueries.add(new NeedStateQueryFactory(dataset).createQuery());
    filterQueries.add(new CreationDateQueryFactory(dataset, 1, ChronoUnit.MONTHS).createQuery());
    filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.NO_HINT_FOR_COUNTERPART)).createQuery());
    if (needModelWrapper.getMatchingContexts() != null && needModelWrapper.getMatchingContexts().size() > 0) {
        filterQueries.add(new MatchingContextQueryFactory(needModelWrapper.getMatchingContexts()).createQuery());
    }
    if (!needModelWrapper.hasFlag(WON.NO_HINT_FOR_ME)) {
        // execute the query
        log.info("query Solr endpoint {} for need {} and need list 1 (without NoHintForCounterpart)", config.getSolrEndpointUri(usedForTesting), needEvent.getUri());
        SolrDocumentList docs = queryExecutor.executeNeedQuery(queryString, null, filterQueries.toArray(new String[filterQueries.size()]));
        if (docs != null) {
            // generate hints for current need (only generate hints for current need, suppress hints for matched needs,
            // perform knee detection depending on current need is WhatsAround or not)
            BulkHintEvent events = null;
            if (needModelWrapper.hasFlag(WON.WHATS_AROUND)) {
                events = hintBuilder.generateHintsFromSearchResult(docs, needEvent, needModelWrapper, false, true, false);
            } else {
                events = hintBuilder.generateHintsFromSearchResult(docs, needEvent, needModelWrapper, false, true, true);
            }
            log.info("Create {} hints for need {} and need list 1 (without NoHintForCounterpart)", events.getHintEvents().size(), needEvent);
            // publish hints to current need
            if (events.getHintEvents().size() != 0) {
                getSender().tell(events, getSelf());
            }
        } else {
            log.warning("No results found for need list 1 (without NoHintForCounterpart) query of need ", needEvent);
        }
    }
    // case 2) needs without NoHintForSelf, excluding WhatsAround needs => hints for needs in index that are not WhatsAround
    filterQueries = new LinkedList<>();
    filterQueries.add(new NeedStateQueryFactory(dataset).createQuery());
    filterQueries.add(new CreationDateQueryFactory(dataset, 1, ChronoUnit.MONTHS).createQuery());
    filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.NO_HINT_FOR_ME)).createQuery());
    filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_AROUND)).createQuery());
    if (needModelWrapper.getMatchingContexts() != null && needModelWrapper.getMatchingContexts().size() > 0) {
        filterQueries.add(new MatchingContextQueryFactory(needModelWrapper.getMatchingContexts()).createQuery());
    }
    if (!needModelWrapper.hasFlag(WON.NO_HINT_FOR_COUNTERPART)) {
        // execute the query
        log.info("query Solr endpoint {} for need {} and need list 2 (without NoHintForSelf, excluding WhatsAround needs)", config.getSolrEndpointUri(usedForTesting), needEvent.getUri());
        SolrDocumentList docs = queryExecutor.executeNeedQuery(queryString, null, filterQueries.toArray(new String[filterQueries.size()]));
        if (docs != null) {
            // generate hints for matched needs (suppress hints for current need, only generate hints for matched needs, perform knee detection)
            BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, needEvent, needModelWrapper, true, false, true);
            log.info("Create {} hints for need {} and need list 2 (without NoHintForSelf, excluding WhatsAround needs)", events.getHintEvents().size(), needEvent);
            // publish hints to current need
            if (events.getHintEvents().size() != 0) {
                getSender().tell(events, getSelf());
            }
        } else {
            log.warning("No results found for need list 2 (without NoHintForSelf, excluding WhatsAround needs) query of need ", needEvent);
        }
    }
    // case 3) needs without NoHintForSelf that are only WhatsAround needs => hints for needs in index that are WhatsAround
    filterQueries = new LinkedList<>();
    filterQueries.add(new NeedStateQueryFactory(dataset).createQuery());
    filterQueries.add(new CreationDateQueryFactory(dataset, 1, ChronoUnit.MONTHS).createQuery());
    filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.NO_HINT_FOR_ME)).createQuery());
    filterQueries.add(new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_AROUND).createQuery());
    if (needModelWrapper.getMatchingContexts() != null && needModelWrapper.getMatchingContexts().size() > 0) {
        filterQueries.add(new MatchingContextQueryFactory(needModelWrapper.getMatchingContexts()).createQuery());
    }
    if (!needModelWrapper.hasFlag(WON.NO_HINT_FOR_COUNTERPART)) {
        // execute the query
        log.info("query Solr endpoint {} for need {} and need list 3 (without NoHintForSelf that are only WhatsAround needs)", config.getSolrEndpointUri(usedForTesting), needEvent.getUri());
        SolrDocumentList docs = queryExecutor.executeNeedQuery(queryString, null, filterQueries.toArray(new String[filterQueries.size()]));
        if (docs != null) {
            // generate hints for matched needs (suppress hints for current need, only generate hints for matched needs, do not perform knee detection)
            BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, needEvent, needModelWrapper, true, false, false);
            log.info("Create {} hints for need {} and need list 3 (without NoHintForSelf that are only WhatsAround needs)", events.getHintEvents().size(), needEvent);
            // publish hints to current need
            if (events.getHintEvents().size() != 0) {
                getSender().tell(events, getSelf());
            }
        } else {
            log.warning("No results found for need list 3 (without NoHintForSelf that are only WhatsAround needs) query of need ", needEvent);
        }
    }
    // index need
    log.info("Add need event content {} to solr index", needEvent);
    needIndexer.index(dataset);
}
Also used : BulkHintEvent(won.matcher.service.common.event.BulkHintEvent) Dataset(org.apache.jena.query.Dataset) NeedModelWrapper(won.protocol.util.NeedModelWrapper) SolrDocumentList(org.apache.solr.common.SolrDocumentList) LinkedList(java.util.LinkedList) SolrMatcherQueryExecutor(won.matcher.solr.query.SolrMatcherQueryExecutor)

Aggregations

LinkedList (java.util.LinkedList)1 Dataset (org.apache.jena.query.Dataset)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 BulkHintEvent (won.matcher.service.common.event.BulkHintEvent)1 SolrMatcherQueryExecutor (won.matcher.solr.query.SolrMatcherQueryExecutor)1 NeedModelWrapper (won.protocol.util.NeedModelWrapper)1