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

org.eclipse.rdf4j.sail.lucene.fn.QueryTupleFunction Maven / Gradle / Ivy

There is a newer version: 3.7.7
Show newest version
/**
 * Copyright (c) 2016 Eclipse RDF4J contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */
package org.eclipse.rdf4j.sail.lucene.fn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.eclipse.rdf4j.common.iteration.CloseableIteratorIteration;
import org.eclipse.rdf4j.common.iteration.ConvertingIteration;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.algebra.evaluation.QueryContext;
import org.eclipse.rdf4j.query.algebra.evaluation.function.TupleFunction;
import org.eclipse.rdf4j.sail.lucene.LuceneSailSchema;
import org.eclipse.rdf4j.sail.lucene.QuerySpec;
import org.eclipse.rdf4j.sail.lucene.SearchIndex;
import org.eclipse.rdf4j.sail.lucene.SearchIndexQueryContextInitializer;
import org.eclipse.rdf4j.sail.lucene.SearchQueryEvaluator;

/**
 * Arguments:
 * 
    *
  1. query is the query string.
  2. *
  3. subject is the query subject or the constant search:allMatches.
  4. *
  5. propertyPredicate is the constant search:property or not present.
  6. *
  7. property is present if only propertyPredicate is present and is the property to query or the constant * search:allProperties.
  8. *
  9. scorePredicate is the constant search:score or not present.
  10. *
  11. snippetPredicate is the constant search:snippet or not present.
  12. *
* Results: *
    *
  1. subject is included if the subject parameter is search:allMatches else omitted.
  2. *
  3. property included if the propertyPredicate parameter is present and the property parameter is * search:allProperties else omitted.
  4. *
  5. score is included if the scorePredicate is search:score else omitted.
  6. *
  7. snippet is included if the snippetPredicate is search:snippet else omitted.
  8. *
* * @deprecated since 3.0. The LucenSpinSail is to be removed in the next major release. */ @Deprecated public class QueryTupleFunction implements TupleFunction { @Override public String getURI() { return LuceneSailSchema.SEARCH.toString(); } @Override public CloseableIteration, QueryEvaluationException> evaluate( ValueFactory valueFactory, Value... args) throws QueryEvaluationException { int i = 0; String queryString = ((Literal) args[i++]).getLabel(); Value nextArg = args[i++]; String matchesVarName = null; IRI subject = null; if (LuceneSailSchema.ALL_MATCHES.equals(nextArg)) { matchesVarName = "matches"; } else { subject = (IRI) nextArg; } String propertyVarName = null; IRI propertyURI = null; String scoreVarName = null; String snippetVarName = null; while (i < args.length) { nextArg = args[i++]; if (LuceneSailSchema.PROPERTY.equals(nextArg)) { nextArg = args[i++]; if (LuceneSailSchema.ALL_PROPERTIES.equals(nextArg)) { propertyVarName = "property"; } else { propertyURI = (IRI) nextArg; } } else if (LuceneSailSchema.SCORE.equals(nextArg)) { scoreVarName = "score"; } else if (LuceneSailSchema.SNIPPET.equals(nextArg)) { snippetVarName = "score"; } } final QuerySpec query = new QuerySpec(matchesVarName, propertyVarName, scoreVarName, snippetVarName, subject, queryString, propertyURI); SearchIndex luceneIndex = SearchIndexQueryContextInitializer.getSearchIndex(QueryContext.getQueryContext()); Collection results = luceneIndex.evaluate((SearchQueryEvaluator) query); return new ConvertingIteration, QueryEvaluationException>( new CloseableIteratorIteration<>(results.iterator())) { @Override protected List convert(BindingSet bindings) throws QueryEvaluationException { List results = new ArrayList<>(4); if (query.getMatchesVariableName() != null) { results.add(bindings.getValue(query.getMatchesVariableName())); } if (query.getPropertyVariableName() != null) { results.add(bindings.getValue(query.getPropertyVariableName())); } if (query.getScoreVariableName() != null) { results.add(bindings.getValue(query.getScoreVariableName())); } if (query.getSnippetVariableName() != null) { results.add(bindings.getValue(query.getSnippetVariableName())); } return results; } }; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy