use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.
the class SiteManagerImpl method findEntities.
@Override
public QueryResultList<Entity> findEntities(FieldQuery query) {
log.debug("findEntities for query{}", query);
//TODO: The QueryResultList expects that the query as executed is added
//to the response. However when executing queries on multiple site they
//might support a different set of features and therefore execute
//different variants. For now I return simple the query as executed by
//the first Site that contributes results
FieldQuery processedQuery = null;
FieldQuery queryWithResults = null;
Set<Entity> entities = new HashSet<Entity>();
for (Site site : referencedSites) {
if (site.supportsSearch()) {
//do not search on sites that do not support it
log.debug(" > query site {}", site.getId());
try {
QueryResultList<Entity> results = site.findEntities(query);
if (processedQuery == null) {
processedQuery = results.getQuery();
}
if (!results.isEmpty() && queryWithResults == null) {
processedQuery = results.getQuery();
}
for (Entity rep : results) {
if (!entities.contains(rep)) {
//do not override
entities.add(rep);
} else {
//TODO: find a solution for this problem
// e.g. allow to add the site for entities
log.info("Entity {} found on more than one Referenced Site" + " -> Representation of Site {} is ignored", rep.getId(), site.getConfiguration().getName());
}
}
} catch (SiteException e) {
log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
}
} else {
log.debug(" > Site {} does not support queries", site.getId());
}
}
return new QueryResultListImpl<Entity>(//use the query with results
queryWithResults != null ? //use the query with results
queryWithResults : //if not a processed
processedQuery != null ? //if not a processed
processedQuery : //else the parsed one
query, entities, Entity.class);
}
use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.
the class BaseGoogleRefineReconcileResource method reconcile.
private JSONObject reconcile(String siteId, ReconcileQuery rQuery) throws JSONException, EntityhubException {
FieldQuery query = createFieldQuery(siteId);
query.addSelectedFields(SELECTED_FIELDS);
addNameConstraint(rQuery, query);
addTypeConstraint(rQuery, query);
addPropertyConstraints(rQuery, query);
query.setLimit(query.getLimit());
QueryResultList<Representation> results = performQuery(siteId, query);
List<JSONObject> jResultList = new ArrayList<JSONObject>(results.size());
//we need to know the highest score to normalise between [0..1]
double maxQueryScore = -1;
if (!results.isEmpty()) {
for (Representation r : results) {
if (maxQueryScore < 0) {
maxQueryScore = r.getFirst(resultScore.getUri(), Number.class).doubleValue();
}
JSONObject jResult = new JSONObject();
jResult.put("id", r.getId());
double similarity = 0.0;
//the name returned for the entity
String name = null;
for (Iterator<Text> labels = r.getText(NAME_FIELD); labels.hasNext(); ) {
Text label = labels.next();
if (label.getText().equalsIgnoreCase(rQuery.getQuery())) {
name = label.getText();
similarity = 1.0;
break;
}
double curSimilarity = Utils.levenshtein(rQuery.getQuery(), label.getText());
if (similarity < curSimilarity) {
name = label.getText();
similarity = curSimilarity;
}
}
//set the selected name
jResult.put("name", name);
Iterator<Reference> types = r.getReferences(TYPE_FIELD);
if (types != null && types.hasNext()) {
jResult.put("type", new JSONArray(ModelUtils.asCollection(types)));
}
double normalisedScore = r.getFirst(resultScore.getUri(), Number.class).doubleValue();
normalisedScore = normalisedScore * similarity / maxQueryScore;
jResult.put("score", normalisedScore);
jResult.put("match", similarity >= 0);
jResultList.add(jResult);
}
}
//else no results ... nothing todo
//sort results based on score
Collections.sort(jResultList, resultScoreComparator);
JSONObject jResultContainer = new JSONObject();
jResultContainer.put("result", new JSONArray(jResultList));
return jResultContainer;
}
use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.
the class JerseyUtils method createFieldQueryForFindRequest.
// /**
// * Returns the {@link FieldQuery} based on the JSON formatted String (in case
// * of "application/x-www-form-urlencoded" requests) or file (in case of
// * "multipart/form-data" requests).<p>
// * @param query the string containing the JSON serialised FieldQuery or
// * <code>null</code> in case of a "multipart/form-data" request
// * @param file the temporary file holding the data parsed by the request to
// * the web server in case of a "multipart/form-data" request or <code>null</code>
// * in case of the "application/x-www-form-urlencoded" request.
// * @return the FieldQuery parsed from the string provided by one of the two
// * parameters
// * @throws WebApplicationException if both parameter are <code>null</code> or
// * if the string provided by both parameters could not be used to parse a
// * {@link FieldQuery} instance.
// */
// public static FieldQuery parseFieldQuery(String query, File file) throws WebApplicationException {
// if(query == null && file == null) {
// throw new WebApplicationException(new IllegalArgumentException("Query Requests MUST define the \"query\" parameter"), Response.Status.BAD_REQUEST);
// }
// FieldQuery fieldQuery = null;
// JSONException exception = null;
// if(query != null){
// try {
// fieldQuery = JSONToFieldQuery.fromJSON(queryFactory,query);
// } catch (JSONException e) {
// log.warn("unable to parse FieldQuery from \"application/x-www-form-urlencoded\" encoded query string "+query,e);
// fieldQuery = null;
// exception = e;
// }
// } //else no query via application/x-www-form-urlencoded parsed
// if(fieldQuery == null && file != null){
// try {
// query = FileUtils.readFileToString(file);
// fieldQuery = JSONToFieldQuery.fromJSON(queryFactory,query);
// } catch (IOException e) {
// throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
// } catch (JSONException e) {
// log.warn("unable to parse FieldQuery from \"multipart/form-data\" encoded query string "+query,e);
// exception = e;
// }
// }//fieldquery already initialised or no query via multipart/form-data parsed
// if(fieldQuery == null){
// throw new WebApplicationException(new IllegalArgumentException("Unable to parse FieldQuery form the parsed query String:"+query, exception),Response.Status.BAD_REQUEST);
// }
// return fieldQuery;
// }
/**
* Creates an {@link FieldQuery} for parameters parsed by the /find requests
* supported by the /symbol, /sites and {siteId} RESTful endpoints.
* TODO: This has to be refactored to use "EntityQuery" as soon as Multiple
* query types are implemented for the Entityhub.
* @param name the name pattern to search entities for (required)
* @param field the field used to search for entities (required)
* @param language the language of the parsed name pattern (optional)
* @param limit the maximum number of result (optional)
* @param offset the offset of the first result (optional)
* @return the {@link FieldQuery} representing the parsed parameter
* @throws WebApplicationException in case the parsed name pattern is invalid.
* The validation of this required parameter provided by the Request is done
* by this method.
* @throws IllegalArgumentException in case the parsed field is invalid. Callers
* of this method need to ensure that this parameter is set to an valid value.
*/
public static FieldQuery createFieldQueryForFindRequest(String name, String field, String language, Integer limit, Integer offset, String ldpath) throws WebApplicationException, IllegalArgumentException {
if (name == null || name.trim().isEmpty()) {
// as an bad Requested sent by the user
throw new WebApplicationException(new IllegalArgumentException("The parsed \"name\" pattern to search entities for MUST NOT be NULL nor EMPTY"), Response.Status.BAD_REQUEST);
} else {
name = name.trim();
}
if (field == null || field.trim().isEmpty()) {
// valid data.
throw new IllegalArgumentException("The parsed search \"field\" MUST NOT be NULL nor EMPTY");
} else {
field = field.trim();
}
log.debug("Process Find Request:");
log.debug(" > name : " + name);
log.debug(" > field : " + field);
log.debug(" > lang : " + language);
log.debug(" > limit : " + limit);
log.debug(" > offset: " + offset);
log.debug(" > ldpath: " + ldpath);
FieldQuery query;
if (ldpath != null && !ldpath.isEmpty()) {
//STANBOL-417
query = new LDPathFieldQueryImpl();
((LDPathFieldQueryImpl) query).setLDPathSelect(ldpath);
} else {
//if no LDPath is parsed select the default field
query = queryFactory.createFieldQuery();
Collection<String> selectedFields = new ArrayList<String>();
//select also the field used to find entities
selectedFields.add(field);
query.addSelectedFields(selectedFields);
}
if (language == null || language.trim().isEmpty()) {
query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false));
} else {
query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false, language));
}
if (limit != null && limit > 0) {
query.setLimit(limit);
}
if (offset != null && offset > 0) {
query.setOffset(offset);
}
return query;
}
use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.
the class EntityhubRootResource method findEntity.
@POST
@Path("/find")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response findEntity(@FormParam(value = "name") String name, @FormParam(value = "field") String parsedField, @FormParam(value = "lang") String language, @FormParam(value = "limit") Integer limit, @FormParam(value = "offset") Integer offset, // solution!
@FormParam(value = "select") String select, @FormParam(value = "ldpath") String ldpath, @Context HttpHeaders headers) {
log.debug("/find Request");
final MediaType acceptedMediaType = getAcceptableMediaType(headers, ENTITY_SUPPORTED_MEDIA_TYPE_INCL_HTML, MediaType.APPLICATION_JSON_TYPE);
if (name == null || name.isEmpty()) {
if (acceptedMediaType.isCompatible(TEXT_HTML_TYPE)) {
//return HTML docu
ResponseBuilder rb = Response.ok(new Viewable("find", this));
rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
} else {
return Response.status(Status.BAD_REQUEST).entity("The name must not be null nor empty for find requests. Missing parameter name.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
} else {
final String property;
if (parsedField == null) {
property = DEFAULT_FIND_FIELD;
} else {
parsedField = parsedField.trim();
if (parsedField.isEmpty()) {
property = DEFAULT_FIND_FIELD;
} else {
property = nsPrefixService.getFullName(parsedField);
if (property == null) {
String messsage = String.format("The prefix '%s' of the parsed field '%' is not " + "mapped to any namespace. Please parse the full URI instead!\n", NamespaceMappingUtils.getPrefix(parsedField), parsedField);
return Response.status(Status.BAD_REQUEST).entity(messsage).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
}
FieldQuery query = JerseyUtils.createFieldQueryForFindRequest(name, property, language, limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, offset, ldpath);
// For the Entityhub we support to select additional fields for results
// of find requests. For the Sites and {site} endpoint this is currently
// deactivated because of very bad performance with OPTIONAL graph patterns
// in SPARQL queries.
Collection<String> additionalSelectedFields = new ArrayList<String>();
if (select != null && !select.isEmpty()) {
for (String selected : select.trim().split(" ")) {
if (selected != null && !selected.isEmpty()) {
additionalSelectedFields.add(selected);
}
}
}
query.addSelectedFields(additionalSelectedFields);
return executeQuery(query, headers, acceptedMediaType);
}
}
use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.
the class SolrYard method find.
private QueryResultList<Representation> find(final FieldQuery parsedQuery, SELECT select) throws YardException {
//create a clone of the query, because we need to refine it because the
//query (as executed) needs to be included in the result set
FieldQuery fieldQuery = parsedQuery.clone();
log.debug("find " + fieldQuery);
long start = System.currentTimeMillis();
final Set<String> selected;
if (select == SELECT.QUERY) {
// if query set the fields to add to the result Representations
selected = new HashSet<String>(fieldQuery.getSelectedFields());
// add the score to query results!
selected.add(RdfResourceEnum.resultScore.getUri());
} else {
// otherwise add all fields
selected = null;
}
final SolrQuery query = solrQueryFactoy.parseFieldQuery(fieldQuery, select);
long queryGeneration = System.currentTimeMillis();
if (closed) {
log.warn("The SolrYard '{}' was already closed!", config.getName());
}
QueryResponse response;
try {
response = AccessController.doPrivileged(new PrivilegedExceptionAction<QueryResponse>() {
public QueryResponse run() throws IOException, SolrServerException {
StreamQueryRequest request = new StreamQueryRequest(query);
return request.process(server);
}
});
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
if (e instanceof SolrServerException) {
if ("unknown handler: /mlt".equals(e.getCause().getMessage())) {
throw new YardException("Solr is missing '<requestHandler name=\"/mlt\"" + " class=\"solr.MoreLikeThisHandler\" startup=\"lazy\" />'" + " in 'solrconfig.xml'", e);
}
throw new YardException("Error while performing Query on SolrServer: " + query.getQuery(), e);
} else if (e instanceof IOException) {
throw new YardException("Unable to access SolrServer", e);
} else {
throw RuntimeException.class.cast(e);
}
}
if (SolrQueryFactory.MLT_QUERY_TYPE.equals(query.getRequestHandler())) {
log.debug("{}", response);
}
long queryTime = System.currentTimeMillis();
// return a queryResultList
QueryResultListImpl<Representation> resultList = new QueryResultListImpl<Representation>(fieldQuery, // by adapting SolrDocuments to Representations
new AdaptingIterator<SolrDocument, Representation>(response.getResults().iterator(), // inline Adapter Implementation
new AdaptingIterator.Adapter<SolrDocument, Representation>() {
@Override
public Representation adapt(SolrDocument doc, Class<Representation> type) {
// use this method for the conversion!
return createRepresentation(doc, selected);
}
}, Representation.class), Representation.class);
long resultProcessing = System.currentTimeMillis();
log.debug(String.format(" ... done [queryGeneration=%dms|queryTime=%dms|resultProcessing=%dms|sum=%dms]", (queryGeneration - start), (queryTime - queryGeneration), (resultProcessing - queryTime), (resultProcessing - start)));
return resultList;
}
Aggregations