Open Semantic Framework PHP API  3.0
 All Data Structures Namespaces Files Functions Variables Pages
SearchQuery.php
Go to the documentation of this file.
1 <?php
2 
5 
11 
56  class SearchQuery extends \StructuredDynamics\osf\php\api\framework\WebServiceQuery
57  {
66  function __construct($network, $appID, $apiKey, $userID)
67  {
68  // Set the OSF network & credentials to use for this query.
69  $this->setNetwork($network);
70  $this->appID = $appID;
71  $this->apiKey = $apiKey;
72  $this->userID = $userID;
73 
74  // Set default configarations for this web service query
75  $this->setSupportedMimes(array("text/xml",
76  "application/json",
77  "application/rdf+xml",
78  "application/rdf+n3",
79  "application/iron+json",
80  "application/iron+csv"));
81 
82  $this->setMethodPost();
83 
84  $this->mime("resultset");
85 
86  $this->setEndpoint("search/");
87 
88  // Set default parameters for this query
89  $this->query("");
90  $this->typesFilters("all");
91  $this->datasetsFilters("all");
92  $this->attributesValuesFilters("all");
94  $this->items(10);
95  $this->page(0);
96  $this->disableInference();
97  $this->includeAggregates();
100  $this->sourceInterface("default");
101  }
102 
114  public function query($query)
115  {
116  $this->params["query"] = urlencode($query);
117 
118  return($this);
119  }
120 
132  public function typesFilters($types)
133  {
134  if(!is_array($types))
135  {
136  $types = array("all");
137  }
138  else
139  {
140  // Encode potential ";" characters
141  foreach($types as $key => $type)
142  {
143  $types[$key] = str_replace(";", "%3B", $type);
144  }
145  }
146 
147  $this->params["types"] = urlencode(implode(";", $types));
148 
149  return($this);
150  }
151 
163  public function typeFilter($type)
164  {
165  $type = str_replace(";", "%3B", $type);
166 
167  if(isset($this->params["types"]) && $this->params["types"] == "all")
168  {
169  $this->params["types"] = "";
170  }
171 
172  if(isset($this->params["types"]) &&
173  $this->params["types"] != "")
174  {
175  $this->params["types"] .= urlencode(";".$type);
176  }
177  else
178  {
179  $this->params["types"] = urlencode($type);
180  }
181 
182  return($this);
183  }
184 
195  public function typeBoost($type, $boostModifier)
196  {
197  $type = str_replace(array(";", "^"), array("%3B", "%5E"), $type);
198 
199  if(isset($this->params["types_boost"]) &&
200  $this->params["types_boost"] != "")
201  {
202  $this->params["types_boost"] .= urlencode(";".$type.'^'.$boostModifier);
203  }
204  else
205  {
206  $this->params["types_boost"] = urlencode($type.'^'.$boostModifier);
207  }
208 
209  return($this);
210  }
211 
223  public function datasetsFilters($datasets)
224  {
225  if(!is_array($datasets) || empty($datasets))
226  {
227  $datasets = array("all");
228  }
229  else
230  {
231  // Encode potential ";" characters
232  foreach($datasets as $key => $dataset)
233  {
234  $datasets[$key] = str_replace(";", "%3B", $dataset);
235  }
236  }
237 
238  $this->params["datasets"] = urlencode(implode(";", $datasets));
239 
240  return($this);
241  }
242 
254  public function datasetFilter($dataset)
255  {
256  if($dataset == '')
257  {
258  return($this);
259  }
260 
261  $dataset = str_replace(";", "%3B", $dataset);
262 
263  if($this->params["datasets"] == "all")
264  {
265  $this->params["datasets"] = "";
266  }
267 
268  if(isset($this->params["datasets"]) &&
269  $this->params["datasets"] != "")
270  {
271  $this->params["datasets"] .= urlencode(";".$dataset);
272  }
273  else
274  {
275  $this->params["datasets"] = urlencode($dataset);
276  }
277 
278  return($this);
279  }
280 
291  public function datasetBoost($dataset, $boostModifier)
292  {
293  $dataset = str_replace(array(";", "^"), array("%3B", "%5E"), $dataset);
294 
295  if(isset($this->params["datasets_boost"]) &&
296  $this->params["datasets_boost"] != "")
297  {
298  $this->params["datasets_boost"] .= urlencode(";".$dataset.'^'.$boostModifier);
299  }
300  else
301  {
302  $this->params["datasets_boost"] = urlencode($dataset.'^'.$boostModifier);
303  }
304 
305  return($this);
306  }
307 
323  public function attributesValuesFilters($attributes="all")
324  {
325  $attrs = array();
326 
327  if(!is_array($attributes))
328  {
329  array_push($attrs, "all");
330  }
331  else
332  {
333  foreach($attributes as $attribute => $values)
334  {
335  $filter = "";
336 
337  if(count($values) == 0)
338  {
339  array_push($attrs, urlencode($attribute));
340  }
341  else
342  {
343  foreach($values as $value)
344  {
345  $attribute = str_replace(";", "%3B", $attribute);
346  $value = str_replace(";", "%3B", $value);
347 
348  $filter = urlencode($attribute)."::".urlencode($value);
349 
350  array_push($attrs, $filter);
351  }
352  }
353  }
354  }
355 
356  $this->params["attributes"] = urlencode(implode(";", $attrs));
357 
358  return($this);
359  }
360 
374  public function attributeValueBoost($attribute, $boostModifier, $value = "", $valueIsUri = FALSE)
375  {
376  $attribute = str_replace(array(";", "^"), array("%3B", "%5E"), $attribute);
377 
378  if(isset($this->params["attributes_boost"]) &&
379  $this->params["attributes_boost"] != "")
380  {
381  $this->params["attributes_boost"] .= urlencode(";".$attribute.($valueIsUri && $value != "" ? "[uri]" : "").($value == '' ? '' : '::'.$value).'^'.$boostModifier);
382  }
383  else
384  {
385  $this->params["attributes_boost"] = urlencode($attribute.($valueIsUri && $value != "" ? "[uri]" : "").($value == '' ? '' : '::'.$value).'^'.$boostModifier);
386  }
387 
388  return($this);
389  }
390 
401  public function attributePhraseBoost($attribute, $boostModifier)
402  {
403  if(isset($this->params["attributes_phrase_boost"]) &&
404  $this->params["attributes_phrase_boost"] != "")
405  {
406  $this->params["attributes_phrase_boost"] .= urlencode(';'.$attribute).'^'.$boostModifier;
407  }
408  else
409  {
410  $this->params["attributes_phrase_boost"] = urlencode($attribute).'^'.$boostModifier;
411  }
412 
413  return($this);
414  }
415 
422  public function phraseBoostDistance($distance)
423  {
424  $this->params["phrase_boost_distance"] = $distance;
425 
426  return($this);
427  }
428 
441  public function attributeValuesFilters($attribute, $values = '')
442  {
443  $attribute = str_replace(";", "%3B", $attribute);
444 
445  if($this->params["attributes"] == "all")
446  {
447  $this->params["attributes"] = "";
448  }
449 
450  if((is_array($values) && count($values) == 0) ||
451  (!is_array($values) && $values == ''))
452  {
453  if(isset($this->params["attributes"]) &&
454  $this->params["attributes"] != "")
455  {
456  $this->params["attributes"] .= urlencode(";".$attribute);
457  }
458  else
459  {
460  $this->params["attributes"] = urlencode($attribute);
461  }
462  }
463  else
464  {
465  if(is_array($values))
466  {
467  foreach($values as $value)
468  {
469  $value = str_replace(";", "%3B", $value);
470 
471  $filter = urlencode($attribute)."::".urlencode($value);
472 
473  if(isset($this->params["attributes"]) &&
474  $this->params["attributes"] != "")
475  {
476  $this->params["attributes"] .= urlencode(";".$filter);
477  }
478  else
479  {
480  $this->params["attributes"] = urlencode($filter);
481  }
482  }
483  }
484  else
485  {
486  $filter = urlencode($attribute)."::".urlencode($values);
487 
488  if(isset($this->params["attributes"]) &&
489  $this->params["attributes"] != "")
490  {
491  $this->params["attributes"] .= urlencode(";".$filter);
492  }
493  else
494  {
495  $this->params["attributes"] = urlencode($filter);
496  }
497  }
498  }
499 
500  return($this);
501  }
502 
512  {
513  $this->params["attributes_boolean_operator"] = "or";
514 
515  return($this);
516  }
517 
528  public function lang($lang = 'en')
529  {
530  $this->params["lang"] = strtolower($lang);
531 
532  return($this);
533  }
534 
546  {
547  $this->params["attributes_boolean_operator"] = "and";
548 
549  return($this);
550  }
551 
567  public function includeAttributes($attributes)
568  {
569  // Encode potential ";" characters
570  foreach($attributes as $key => $attribute)
571  {
572  $attributes[$key] = str_replace(";", "%3B", $attribute);
573  }
574 
575  $this->params["include_attributes_list"] = urlencode(implode(";", $attributes));
576 
577  return($this);
578  }
579 
595  public function includeAttribute($attribute)
596  {
597  // Encode potential ";" characters
598  $attribute = str_replace(";", "%3B", $attribute);
599 
600  if(isset($this->params["include_attributes_list"]) &&
601  $this->params["include_attributes_list"] != "")
602  {
603  $this->params["include_attributes_list"] .= urlencode(";".$attribute);
604  }
605  else
606  {
607  $this->params["include_attributes_list"] = urlencode($attribute);
608  }
609 
610  return($this);
611  }
612 
624  public function includeNoAttributes()
625  {
626  $this->params["include_attributes_list"] = "none";
627 
628  return($this);
629  }
630 
642  public function items($items)
643  {
644  if($items < 0)
645  {
646  $items = 0;
647  }
648 
649  $this->params["items"] = $items;
650 
651  return($this);
652  }
653 
666  public function page($page)
667  {
668  if($page < 0)
669  {
670  $page = 0;
671  }
672 
673  $this->params["page"] = $page;
674 
675  return($this);
676  }
677 
687  public function enableInference()
688  {
689  $this->params["inference"] = "on";
690 
691  return($this);
692  }
693 
701  public function disableInference()
702  {
703  $this->params["inference"] = "off";
704 
705  return($this);
706  }
707 
715  public function defaultOperatorAND()
716  {
717  $this->params["default_operator"] = "and";
718 
719  return($this);
720  }
721 
734  public function defaultOperatorOR($constrains)
735  {
736  $this->params["default_operator"] = "or::".urlencode($constrains);
737 
738  return($this);
739  }
740 
748  public function excludeAggregates()
749  {
750  $this->params["include_aggregates"] = "false";
751 
752  return($this);
753  }
754 
764  public function includeAggregates()
765  {
766  $this->params["include_aggregates"] = "true";
767 
768  return($this);
769  }
770 
783  public function aggregateAttributes($attributes)
784  {
785  // Encode potential ";" characters
786  foreach($attributes as $key => $attribute)
787  {
788  $attributes[$key] = str_replace(";", "%3B", $attribute);
789  }
790 
791  $this->params["aggregate_attributes"] = urlencode(implode(";", $attributes));
792 
793  return($this);
794  }
795 
808  public function aggregateAttribute($attribute)
809  {
810  // Encode potential ";" characters
811  if(isset($this->params["aggregate_attributes"]) &&
812  $this->params["aggregate_attributes"] != "")
813  {
814  $this->params["aggregate_attributes"] .= urlencode(";".$attribute);
815  }
816  else
817  {
818  $this->params["aggregate_attributes"] = urlencode($attribute);
819  }
820 
821  return($this);
822  }
823 
836  {
837  $this->params["aggregate_attributes_object_type"] = "literal";
838 
839  return($this);
840  }
841 
850  {
851  $this->params["aggregate_attributes_object_type"] = "uriliteral";
852 
853  return($this);
854  }
855 
866  {
867  $this->params["aggregate_attributes_object_type"] = "uri";
868 
869  return($this);
870  }
871 
873  {
874  if($nb < -1)
875  {
876  $nb = -1;
877  }
878 
879  $this->params["aggregate_attributes_object_nb"] = $nb;
880 
881  return($this);
882  }
883 
899  public function distanceFilter($lat, $long, $distance, $distanceType = "km")
900  {
901  if($distance <= 0)
902  {
903  $distance = 1;
904  }
905 
906  if($distanceType != "km" && $distanceType != "mile")
907  {
908  $distanceType = "km";
909  }
910 
911  $type = "";
912 
913  switch($distanceType)
914  {
915  case "km":
916  $type = "0";
917  break;
918 
919  case "mile":
920  $type = "1";
921  break;
922  }
923 
924  $this->params["distance_filter"] = "$lat;$long;$distance;$type";
925 
926  return($this);
927  }
928 
945  public function rangeFilter($topLeftLat, $topLeftLong, $bottomRightLat, $bottomRightLong)
946  {
947  $this->params["range_filter"] = "$topLeftLat;$topLeftLong;$bottomRightLat;$bottomRightLong";
948 
949  return($this);
950  }
951 
962  public function sort($sortProperty, $sortOrder)
963  {
964  if(!isset($this->params["sort"]))
965  {
966  $this->params["sort"] = "";
967  }
968 
969  if($this->params["sort"] != "")
970  {
971  $this->params["sort"] .= ";";
972  }
973 
974  $this->params["sort"] .= urlencode($sortProperty." ".strtolower($sortOrder));
975 
976  return($this);
977  }
978 
1011  public function extendedFilters($filters)
1012  {
1013  if(!isset($this->params["extended_filters"]))
1014  {
1015  $this->params["extended_filters"] = "";
1016  }
1017 
1018  $this->params["extended_filters"] = $filters;
1019 
1020  return($this);
1021  }
1022 
1027  public function includeScores()
1028  {
1029  $this->params["include_scores"] = TRUE;
1030 
1031  return($this);
1032  }
1033 
1037  public function excludeScores()
1038  {
1039  $this->params["include_scores"] = FALSE;
1040 
1041  return($this);
1042  }
1043 
1044 
1048  public function includeSpellcheck()
1049  {
1050  $this->params["spellcheck"] = TRUE;
1051 
1052  return($this);
1053  }
1054 
1058  public function excludeSpellcheck()
1059  {
1060  $this->params["spellcheck"] = FALSE;
1061 
1062  return($this);
1063  }
1064 
1072  public function searchRestriction($property, $boost = 1)
1073  {
1074  if(!isset($this->params["search_restrictions"]))
1075  {
1076  $this->params["search_restrictions"] = "";
1077  }
1078 
1079  if(!empty($this->params["search_restrictions"]))
1080  {
1081  $this->params["search_restrictions"] .= ';';
1082  }
1083 
1084  $this->params["search_restrictions"] .= urlencode($property).'^'.$boost;
1085 
1086  return($this);
1087  }
1088 
1098  public function recordsLocationAggregator($lat, $long)
1099  {
1100  if(!isset($this->params["results_location_aggregator"]))
1101  {
1102  $this->params["results_location_aggregator"] = "";
1103  }
1104 
1105  $this->params["results_location_aggregator"] .= $lat.','.$long;
1106 
1107  return($this);
1108  }
1109  }
1110 
1112 ?>
setAggregateAttributeObjectTypeToUri()
Determines that the aggregated value returned by the endpoint is a URI.
setAggregateAttributesObjectTypeToLiteral()
Determines that the aggregated value returned by the endpoint is a literal.
recordsLocationAggregator($lat, $long)
Specifies a lat/long location where all the results should be aggregated around.
phraseBoostDistance($distance)
Define the maximum distance between the keywords of the search query that is used by the attributePhr...
includeNoAttributes()
Specify that no attributes should be returned by the query.
excludeAggregates()
Exclude the aggregate records in the resultset of this query.
defaultOperatorOR($constrains)
Set the default search query operator to OR.
setAttributesBooleanOperatorToOr()
Set the attributes boolean operator to OR.
page($page)
Set the offset of the resultset to return.
typeBoost($type, $boostModifier)
Modifying the score of the results returned by the Search endpoint by boosting the results that have ...
typeFilter($type)
Add a type filter to use for this search query.
searchRestriction($property, $boost=1)
Include the scores of the results into the resultset.
enableInference()
Enable the inference for this query.
items($items)
Set the number of items to return in a single resultset.
attributeValuesFilters($attribute, $values= '')
Set an attribute/value(s) filter to use for this search query.
datasetFilter($dataset)
Add a dataset filter to use for this search query.
rangeFilter($topLeftLat, $topLeftLong, $bottomRightLat, $bottomRightLong)
The range filter is a series of parameter that are used to filter records of the dataset according to...
datasetBoost($dataset, $boostModifier)
Modifying the score of the results returned by the Search endpoint by boosting the results that have ...
includeSpellcheck()
Includes the spellchecking suggestions to the resultset in the case that the resultset is empty...
attributePhraseBoost($attribute, $boostModifier)
Modifying the score of the results returned by the Search endpoint by boosting the results where the ...
distanceFilter($lat, $long, $distance, $distanceType="km")
The distance filter is a series of parameter that are used to filter records of the dataset according...
includeAggregates()
Include the aggregate records in the resultset of this query.
excludeSpellcheck()
Includes the spellchecking suggestions to the resultset even if the resultset is empty.
lang($lang= 'en')
Set the language of the records to be returned by the search endpoint.
The Search Web service is used to perform full text searches on the structured data indexed on a OSF ...
Definition: SearchQuery.php:56
attributesValuesFilters($attributes="all")
Set all the attribute/value filters to use for this search query.
includeAttribute($attribute)
Set an attribute URI to include in the resultset returned by the search endpoint. ...
__construct($network, $appID, $apiKey, $userID)
Constructor.
Definition: SearchQuery.php:66
aggregateAttributes($attributes)
Specify a set of attributes URI for which we want their aggregated values.
includeScores()
Include the scores of the results into the resultset.
setAggregateAttributesObjectTypeToUriLiteral()
Determines that the aggregated value returned by the endpoint is a URI and a Literal.
setAttributesBooleanOperatorToAnd()
Set the attributes boolean operator to AND.
excludeScores()
Exclude the scores of the results into the resultset.
sort($sortProperty, $sortOrder)
Add a sort criteria to the Search query.
includeAttributes($attributes)
Set a list of attribute URIs to include in the resultset returned by the search endpoint.
query($query)
Set the keywords to use for this search query.
attributeValueBoost($attribute, $boostModifier, $value="", $valueIsUri=FALSE)
Modifying the score of the results returned by the Search endpoint by boosting the results that have ...
extendedFilters($filters)
Extended filters are used to define more complex search filtered searches.
datasetsFilters($datasets)
Set all the dataset filters to use for this search query.
aggregateAttribute($attribute)
Specify an attribute URI for which we want its aggregated values.
disableInference()
Disable the inference for this query.
typesFilters($types)
Set all the type filters to use for this search query.
defaultOperatorAND()
Set the default search query operator to AND.