Search in sources :

Example 1 with HasFlagQueryFactory

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

the class SolrMatcherActor method processActiveAtomEvent.

protected void processActiveAtomEvent(AtomEvent atomEvent) throws IOException, SolrServerException, JsonLdError {
    log.info("Start processing active atom event {}", atomEvent);
    // check if the atom has doNotMatch flag, then do not use it for querying or
    // indexing
    Dataset dataset = atomEvent.deserializeAtomDataset();
    AtomModelWrapper atomModelWrapper = new AtomModelWrapper(dataset);
    if (atomModelWrapper.flag(WONMATCH.NoHintForMe) && atomModelWrapper.flag(WONMATCH.NoHintForCounterpart)) {
        log.info("Discarding received atom due to flags match:NoHintForMe and match:NoHintForCounterpart: {}", atomEvent);
        return;
    }
    // check if atom has a sparql query attached
    if (atomModelWrapper.sparqlQuery()) {
        log.debug("Atom {} has a sparql query, omitting this atom in Solr matcher", atomModelWrapper.getAtomUri());
        return;
    }
    // check if atom is usedForTesting only
    boolean usedForTesting = atomModelWrapper.flag(WONMATCH.UsedForTesting);
    SolrMatcherQueryExecutor queryExecutor = (usedForTesting ? testQueryExecuter : defaultQueryExecuter);
    // create another query depending if the current atom is "WhatsAround" or a
    // default atom
    String queryString = null;
    if (atomModelWrapper.flag(WONMATCH.WhatsAround)) {
        // WhatsAround doesnt match on terms only other atoms in close location are
        // boosted
        WhatsAroundQueryFactory qf = new WhatsAroundQueryFactory(dataset);
        queryString = qf.createQuery();
    } else if (atomModelWrapper.flag(WONMATCH.WhatsNew)) {
        WhatsNewQueryFactory qf = new WhatsNewQueryFactory(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 atoms
        DefaultAtomQueryFactory qf = new DefaultAtomQueryFactory(dataset);
        queryString = qf.createQuery();
    }
    // add filters to the query: default filters are
    // - atom 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 atoms:
    // 1) atoms without NoHintForCounterpart => hints for current atom
    // 2) atoms without NoHintForSelf, excluding WhatsAround atoms => hints for
    // atoms in index that are not WhatsAround
    // 3) atoms without NoHintForSelf that are only WhatsAround atoms => hints for
    // atoms in index that are WhatsAround
    // to achieve this use a different filters for these queries
    // case 1) atoms without NoHintForCounterpart => hints for current atom
    List<String> filterQueries = new LinkedList<>();
    filterQueries.add(new AtomStateQueryFactory(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 (atomModelWrapper.getMatchingContexts() != null && atomModelWrapper.getMatchingContexts().size() > 0) {
        filterQueries.add(new MatchingContextQueryFactory(atomModelWrapper.getMatchingContexts()).createQuery());
    }
    if (!atomModelWrapper.flag(WONMATCH.NoHintForMe)) {
        // execute the query
        log.info("query Solr endpoint {} for atom {} and atom list 1 (without NoHintForCounterpart)", config.getSolrEndpointUri(usedForTesting), atomEvent.getUri());
        SolrDocumentList docs = queryExecutor.executeAtomQuery(queryString, config.getMaxHints(), null, filterQueries.toArray(new String[filterQueries.size()]));
        if (docs != null) {
            // perform knee detection depending on current atom is WhatsAround/WhatsNew or
            // not)
            boolean kneeDetection = atomModelWrapper.flag(WONMATCH.WhatsNew) || atomModelWrapper.flag(WONMATCH.WhatsAround) ? false : true;
            // generate hints for current atom (only generate hints for current atom,
            // suppress hints for matched atoms,
            BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, atomEvent, atomModelWrapper, false, true, kneeDetection);
            log.info("Create {} hints for atom {} and atom list 1 (without NoHintForCounterpart)", events.getHintEvents().size(), atomEvent);
            // publish hints to current atom
            if (events.getHintEvents().size() != 0) {
                getSender().tell(events, getSelf());
            }
        } else {
            log.warning("No results found for atom list 1 (without NoHintForCounterpart) query of atom ", atomEvent);
        }
    }
    // case 2) atoms without NoHintForSelf, excluding WhatsAround atoms => hints for
    // atoms in index that are not WhatsAround
    filterQueries = new LinkedList<>();
    filterQueries.add(new AtomStateQueryFactory(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());
    filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_NEW)).createQuery());
    if (atomModelWrapper.getMatchingContexts() != null && atomModelWrapper.getMatchingContexts().size() > 0) {
        filterQueries.add(new MatchingContextQueryFactory(atomModelWrapper.getMatchingContexts()).createQuery());
    }
    if (!atomModelWrapper.flag(WONMATCH.NoHintForCounterpart)) {
        // execute the query
        log.info("query Solr endpoint {} for atom {} and atom list 2 (without NoHintForSelf, excluding WhatsAround atoms)", config.getSolrEndpointUri(usedForTesting), atomEvent.getUri());
        SolrDocumentList docs = queryExecutor.executeAtomQuery(queryString, config.getMaxHintsForCounterparts(), null, filterQueries.toArray(new String[filterQueries.size()]));
        if (docs != null) {
            // generate hints for matched atoms (suppress hints for current atom, only
            // generate hints for matched atoms, perform knee detection)
            BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, atomEvent, atomModelWrapper, true, false, true);
            log.info("Create {} hints for atom {} and atom list 2 (without NoHintForSelf, excluding WhatsAround atoms)", events.getHintEvents().size(), atomEvent);
            // publish hints to current atom
            if (events.getHintEvents().size() != 0) {
                getSender().tell(events, getSelf());
            }
        } else {
            log.warning("No results found for atom list 2 (without NoHintForSelf, excluding WhatsAround atoms) query of atom ", atomEvent);
        }
    }
    // case 3) atoms without NoHintForSelf that are only WhatsAround atoms => hints
    // for atoms in index that are WhatsAround
    filterQueries = new LinkedList<>();
    filterQueries.add(new AtomStateQueryFactory(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.OR, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_AROUND), new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_NEW)).createQuery());
    if (atomModelWrapper.getMatchingContexts() != null && atomModelWrapper.getMatchingContexts().size() > 0) {
        filterQueries.add(new MatchingContextQueryFactory(atomModelWrapper.getMatchingContexts()).createQuery());
    }
    if (!atomModelWrapper.flag(WONMATCH.NoHintForCounterpart)) {
        // hints for WhatsAround Atoms should not have the keywords from title,
        // description, tags etc.
        // this can prevent to actually find WhatsAround atoms.
        // Instead create a WhatsAround query (query without keywords, just location) to
        // find other WhatsAround atoms
        queryString = (new WhatsAroundQueryFactory(dataset)).createQuery();
        // execute the query
        log.info("query Solr endpoint {} for atom {} and atom list 3 (without NoHintForSelf that are only WhatsAround atoms)", config.getSolrEndpointUri(usedForTesting), atomEvent.getUri());
        SolrDocumentList docs = queryExecutor.executeAtomQuery(queryString, config.getMaxHintsForCounterparts(), null, filterQueries.toArray(new String[filterQueries.size()]));
        if (docs != null) {
            // generate hints for matched atoms (suppress hints for current atom, only
            // generate hints for matched atoms, do not perform knee detection)
            BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, atomEvent, atomModelWrapper, true, false, false);
            log.info("Create {} hints for atom {} and atom list 3 (without NoHintForSelf that are only WhatsAround atoms)", events.getHintEvents().size(), atomEvent);
            // publish hints to current atom
            if (events.getHintEvents().size() != 0) {
                getSender().tell(events, getSelf());
            }
        } else {
            log.warning("No results found for atom list 3 (without NoHintForSelf that are only WhatsAround atoms) query of atom ", atomEvent);
        }
    }
    // index atom
    log.info("Add atom event content {} to solr index", atomEvent);
    atomIndexer.index(dataset);
}
Also used : BulkHintEvent(won.matcher.service.common.event.BulkHintEvent) Dataset(org.apache.jena.query.Dataset) WhatsAroundQueryFactory(won.matcher.solr.query.factory.WhatsAroundQueryFactory) AtomStateQueryFactory(won.matcher.solr.query.factory.AtomStateQueryFactory) DefaultAtomQueryFactory(won.matcher.solr.query.factory.DefaultAtomQueryFactory) SolrDocumentList(org.apache.solr.common.SolrDocumentList) BooleanQueryFactory(won.matcher.solr.query.factory.BooleanQueryFactory) LinkedList(java.util.LinkedList) CreationDateQueryFactory(won.matcher.solr.query.factory.CreationDateQueryFactory) WhatsNewQueryFactory(won.matcher.solr.query.factory.WhatsNewQueryFactory) MatchingContextQueryFactory(won.matcher.solr.query.factory.MatchingContextQueryFactory) AtomModelWrapper(won.protocol.util.AtomModelWrapper) SolrMatcherQueryExecutor(won.matcher.solr.query.SolrMatcherQueryExecutor) HasFlagQueryFactory(won.matcher.solr.query.factory.HasFlagQueryFactory)

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 AtomStateQueryFactory (won.matcher.solr.query.factory.AtomStateQueryFactory)1 BooleanQueryFactory (won.matcher.solr.query.factory.BooleanQueryFactory)1 CreationDateQueryFactory (won.matcher.solr.query.factory.CreationDateQueryFactory)1 DefaultAtomQueryFactory (won.matcher.solr.query.factory.DefaultAtomQueryFactory)1 HasFlagQueryFactory (won.matcher.solr.query.factory.HasFlagQueryFactory)1 MatchingContextQueryFactory (won.matcher.solr.query.factory.MatchingContextQueryFactory)1 WhatsAroundQueryFactory (won.matcher.solr.query.factory.WhatsAroundQueryFactory)1 WhatsNewQueryFactory (won.matcher.solr.query.factory.WhatsNewQueryFactory)1 AtomModelWrapper (won.protocol.util.AtomModelWrapper)1