Difference between revisions of "API:EPrints/Search"

From EPrints Documentation
Jump to: navigation, search
(New page: <!-- Pod2Wiki=_preamble_ This page has been automatically generated from the EPrints source. Any wiki changes made between the 'Pod2Wiki=*' and 'End of Pod2Wiki' comments will be lost. -...)
 
(add_field)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
<!-- Pod2Wiki=_preamble_  
 
<!-- Pod2Wiki=_preamble_  
This page has been automatically generated from the EPrints source. Any wiki changes made between the 'Pod2Wiki=*' and 'End of Pod2Wiki' comments will be lost.
+
This page has been automatically generated from the EPrints 3.2 source. Any wiki changes made between the 'Pod2Wiki=*' and 'Edit below this comment' comments will be lost.
  -->{{Pod2Wiki}}{{API:Source|file=EPrints/Search.pm|package_name=EPrints::Search}}[[Category:API|Search]]<!-- End of Pod2Wiki -->
+
  -->{{API}}{{Pod2Wiki}}{{API:Source|file=perl_lib/EPrints/Search.pm|package_name=EPrints::Search}}[[Category:API|SEARCH]][[Category:API:EPrints/Search|SEARCH]]<div><!-- Edit below this comment -->
<!-- Pod2Wiki=head_name -->=NAME=
 
'''EPrints::Search''' - Represents a single search
 
  
<!-- End of Pod2Wiki -->
 
<!-- Pod2Wiki=head_description -->=DESCRIPTION=
 
The Search object represents the conditions of a single  search.
 
  
It used to also store the results of the search, but now it returns an [[API:EPrints/List|EPrints::List]] object.  
+
<!-- Pod2Wiki=_private_ --><!-- Pod2Wiki=head_name -->
 +
==NAME==
 +
EPrints::Search - retrieve objects based on search criteria
 +
 
 +
<!-- Edit below this comment -->
 +
 
 +
 
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_description -->
 +
==DESCRIPTION==
 +
The Search object represents the conditions of a single search.
 +
 
 +
Executing a search returns an [[API:EPrints/List|EPrints::List]] object.
  
 
A search expression can also render itself as a web-form, populate itself with values from that web-form and render the results as a web page.
 
A search expression can also render itself as a web-form, populate itself with values from that web-form and render the results as a web page.
  
<!-- End of Pod2Wiki -->
+
[[API:EPrints/Plugin/Search|EPrints::Plugin::Search]] provides a pluggable architecture for searching EPrints.
<!-- Pod2Wiki=head_examples -->=EXAMPLES=
+
 
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=head_searching_for_eprints -->==Searching for Eprints==
+
 
  $ds = $session-&gt;get_repository-&gt;get_dataset( "archive" );
+
 
 
+
<!-- Pod2Wiki= -->
  $searchexp = EPrints::Search-&gt;new(
+
<!-- Pod2Wiki=head_synopsis -->
    satisfy_all =&gt; 1,
+
==SYNOPSIS==
    session =&gt; $session,
+
<!-- Edit below this comment -->
    dataset =&gt; $ds,
+
 
  );
+
 
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_searching_for_eprints -->
 +
===Searching for Eprints===
 +
<pre>  $ds = $repo-&gt;dataset( "archive" );
 
    
 
    
   # Search for an eprint with eprintid 23
+
   # NB 'archive' is an implicit filter on eprint.status
  # (ought to use EPrints::DataObj::EPrint-&gt;new( SESSION, ID ))
+
   $list = $ds->search(filters => [{
   $searchexp-&gt;add_field( $ds-&gt;get_field( "eprintid" ), 23 );
+
    meta_fields => [qw( eprintid )], value => 23,
 +
  }]);
 
    
 
    
   $searchexp-&gt;add_field( $ds-&gt;get_field( "creators" ), "John Smith" );
+
   $list = $ds->search(search_fields => [{
 +
    meta_fields => [qw( creators_name )], value => "John Smith",
 +
  }]);
 
    
 
    
<!-- End of Pod2Wiki -->
+
  $searchexp = $ds->prepare_search();
<!-- Pod2Wiki=head_getting_results -->==Getting Results==
+
  $searchexp->add_field(
  $results = $searchexp-&gt;perform_search;
+
    fields => [
 +
      $ds->field('creators_name')
 +
    ],
 +
    value => "John Smith",
 +
    match => "EQ", # EQuals
 +
  );
 +
  $searchexp->add_field(
 +
    fields => [
 +
      $ds->field('title')
 +
    ],
 +
    value => "eagle buzzard",
 +
    match => "IN", # INdex
 +
  );</pre>
 +
 
 +
See [[API:EPrints/DataSet|EPrints::DataSet]] for more API methods for constructing search objects.
 +
 
 +
<!-- Edit below this comment -->
 +
 
 +
 
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_getting_results -->
 +
===Getting Results===
 +
<pre>  $list = $searchexp-&gt;perform_search;
 
    
 
    
   my $count = $searchexp-&gt;count;
+
   my $count = $list->count;
  my $count = $results-&gt;count;
 
 
    
 
    
   my $ids = $results-&gt;get_ids( 0, 10 );
+
   my $ids = $results->ids( 0, 10 );
   my $ids = $results-&gt;get_ids; # Get all matching ids
+
   my $ids = $results->ids; # Get all matching ids
 
    
 
    
   my $info = { matches =&gt; 0 };
+
   my $info = { matches => 0 };
 
   sub fn {
 
   sub fn {
 
     my( $session, $dataset, $eprint, $info ) = @_;
 
     my( $session, $dataset, $eprint, $info ) = @_;
     $info-&gt;{matches}++;
+
     $info->{matches}++;
 
   };
 
   };
   $results-&gt;map( \&amp;fn, $info );
+
   $list->map( \&fn, $info );</pre>
 
+
 
  $searchexp-&gt;dispose;
 
 
 
 
See [[API:EPrints/List|EPrints::List]] for more.
 
See [[API:EPrints/List|EPrints::List]] for more.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=head_methods -->=METHODS=
+
 
<!-- End of Pod2Wiki -->
+
 
<!-- Pod2Wiki=item_new -->==new==
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_methods -->
 +
==METHODS==
 +
<!-- Pod2Wiki=head_new -->
 +
===new===
  
  $searchexp = EPrints::Search-&gt;new( %params )
+
<source lang="perl">$searchexp = EPrints::Search->new( %params )
  
 +
</source>
 
Create a new search expression.
 
Create a new search expression.
  
Line 65: Line 103:
 
GENERAL PARAMETERS
 
GENERAL PARAMETERS
  
<!-- End of Pod2Wiki -->
+
* session (required)
<!-- Pod2Wiki=item_session -->==session==
+
: The current [[API:EPrints/Session|EPrints::Session]]
  
  session (required)
+
* dataset OR dataset_id (required)
 +
: Either the [[API:EPrints/DataSet|EPrints::DataSet]] to search, or the ID of it.
  
The current [[API:EPrints/Session|EPrints::Session]]
+
* allow_blank (default 0)
 +
: Unless this is set, a search with no conditions will return zero records  rather than all records.
  
<!-- End of Pod2Wiki -->
+
* satisfy_all (default 1)
<!-- Pod2Wiki=item_dataset_id -->==dataset_id==
+
: If this is true than all search-fields much be satisfied, if false then  results matching any search-field will be returned.
  
  dataset OR dataset_id (required)
+
* search_fields
 +
: A reference to an array of search field configuration structures. Each  takes the form { id=&gt;"...", default=&gt;"..", meta_fields=&gt;"..." } where the meaning is the same as for search configuration in ArchiveConfig.
  
Either the [[API:EPrints/DataSet|EPrints::DataSet]] to search, or the ID of it.
+
: Search fields can also be added to the search expression after it has been constructed.
  
<!-- End of Pod2Wiki -->
+
* order
<!-- Pod2Wiki=item_allow_blank -->==allow_blank==
+
: The order the results should be returned. This is a key to the list of orders available to this dataset, defined in ArchiveConfig.pm
  
  allow_blank (default 0)
+
* custom_order
 +
: "order" limits you to the orders specified in ArchiveConfig, and is usually used by the web page based searching. custom_order allows you to specify any order you like. The format is  foo/-bar. This means that the results will be sorted by foo and then any with equal foo values will be reverse sorted by bar. More than 2 fields can be specified.
  
Unless this is set, a search with no conditions will return zero records  rather than all records.
+
* keep_cache
 +
: If true then the search results produced will be stored in the database even after the current script ends. This is useful for speeding up  page 2 onwards of a search.
  
<!-- End of Pod2Wiki -->
+
: keep_cache may get set to true anyway for various reasons, but setting the parameter makes certain of it.
<!-- Pod2Wiki=item_satisfy_all -->==satisfy_all==
 
  
  satisfy_all (default 1)
+
* cache_id
 +
: The ID of a cached search. The cache contains both the results of the search, and the parameters used for the search.
  
If this is true than all search-fields much be satisfied, if false then results matching any search-field will be returned.
+
: If the cache still exists, it will set the default values of the  search fields, and when the search is performed it will skip the search and build a search results object directly from the cache.
  
<!-- End of Pod2Wiki -->
+
* limit
<!-- Pod2Wiki=item_search_fields -->==search_fields==
+
: Limit the number of matching records to limit.
  
A reference to an array of search field configuration structures. Each  takes the form { id=&gt;"...", default=&gt;"..", meta_fields=&gt;"..." } where the meaning is the same as for search configuration in ArchiveConfig.
+
WEB PAGE RELATED PARAMETERS
  
Search fields can also be added to the search expression after it has been constructed.
+
* prefix (default "")
 +
: When generating the web form and reading back from the web form, the prefix is inserted before the form names of all fields. This is useful if you need to put two search expressions in a single form for some reason.
  
<!-- End of Pod2Wiki -->
+
* staff (default 0)
<!-- Pod2Wiki=item_order -->==order==
+
: If true then this is a "staff" search, which prevents searching unless the user is staff, and the results link to the staff URL of an item rather than the public URL.
  
The order the results should be returned. This is a key to the list of orders available to this dataset, defined in ArchiveConfig.pm
+
* filters
 +
: A reference to an array of filter definitions.
  
<!-- End of Pod2Wiki -->
+
: Filter definitions take the form of: { value=&gt;"..", match=&gt;"..", merge=&gt;"..", id=&gt;".." } and work much like normal search fields except that they do not appear in the web form so force certain search parameters on the user.
<!-- Pod2Wiki=item_custom_order -->==custom_order==
 
  
"order" limits you to the orders specified in ArchiveConfig, and is usually used by the web page based searching. custom_order allows you to specify any order you like. The format is  foo/-bar. This means that the results will be sorted by foo and then any with equal foo values will be reverse sorted by bar. More than 2 fields can be specified.
+
: An optional parameter of describe=&gt;0 can be set to supress the filter being mentioned in the description of the search.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_keep_cache -->==keep_cache==
 
  
If true then the search results produced will be stored in the database even after the current script ends. This is useful for speeding up  page 2 onwards of a search.
 
  
keep_cache may get set to true anyway for various reasons, but setting the parameter makes certain of it.
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_from_cache -->
 +
===from_cache===
  
<!-- End of Pod2Wiki -->
+
<source lang="perl">$ok = $thing->from_cache( $id )
<!-- Pod2Wiki=item_cache_id -->==cache_id==
 
  
The ID of a cached search. The cache contains both the results of the search, and the parameters used for the search.
+
</source>
 +
Populate this search expression with values from the given cache.
  
If the cache still exists, it will set the default values of the  search fields, and when the search is performed it will skip the  search and build a search results object directly from the cache.
+
Return false if the cache does not exist.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_prefix -->==prefix==
 
  
  prefix (default "")
 
  
When generating the web form and reading back from the web form, the prefix is inserted before the form names of all fields. This is useful if you need to put two search expressions in a single form for some reason.
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_add_field -->
 +
===add_field===
  
<!-- End of Pod2Wiki -->
+
<source lang="perl">$searchfield = $searchexp->add_field( %opts )
<!-- Pod2Wiki=item_staff -->==staff==
 
  
   staff (default 0)
+
</source>
 +
<pre>  fields - one or more fields to search over
 +
   match - match type
 +
  merge - merge type
 +
  value - value to match against (for EX matches, NULL = is_null!)
 +
  id - search field id, if not the name of the first field
 +
  filter - is filter-type
 +
  show_help - show help in search input</pre>
  
If true then this is a "staff" search, which prevents searching unless the user is staff, and the results link to the staff URL of an item rather than the public URL.
+
Adds a new search in $fields which is either a single [[API:EPrints/MetaField|EPrints::MetaField]] or a list of fields in an array ref with default $value. If a search field already exists, the value of that field is replaced with $value.
  
<!-- End of Pod2Wiki -->
+
See [[API:EPrints/Search/Field|EPrints::Search::Field]] for details on match/merge etc.
<!-- Pod2Wiki=item_filters -->==filters==
 
  
A reference to an array of filter definitions.
+
<!-- Edit below this comment -->
  
Filter definitions take the form of: { value=&gt;"..", match=&gt;"..", merge=&gt;"..", id=&gt;".." } and work much like normal search fields except that they do not appear in the web form so force certain search parameters on the user.
+
Note relating to the <code>for EX matches, NULL = is_null</code> documentation above.
  
An optional parameter of describe=&gt;0 can be set to supress the filter being mentioned in the description of the search.
+
To search for items with no value, use something similar to this - explicitly setting the value to undef.:
 +
<source lang="perl">
 +
### Example: search for items in the review area with no date set.
 +
my $ds = $session->dataset( "buffer" );
 +
my $searchexp = $ds->prepare_search();
 +
$searchexp->add_field(
 +
    fields => [
 +
      $ds->field( 'date' ),
 +
    ],
 +
    value => undef,
 +
    match => "EX",
 +
);
 +
</source>
  
<!-- End of Pod2Wiki -->
 
<!-- Pod2Wiki=item_from_cache -->==from_cache==
 
  
  $ok = $thing-&gt;from_cache( $id )
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_clear -->
  
Populate this search expression with values from the given cache.
+
===clear===
  
Return false if the cache does not exist.
+
<source lang="perl">$searchexp->clear
  
<!-- End of Pod2Wiki -->
+
</source>
<!-- Pod2Wiki=item_add_field -->==add_field==
+
Clear the search values of all search fields in the expression.
  
  $searchfield = $searchexp-&gt;add_field( $metafields, $value, $match, $merge, $id, $filter, $show_help )
+
Resets satisfy_all to true.
  
Adds a new search in $metafields which is either a single [[API:EPrints/MetaField|EPrints::MetaField]] or a list of fields in an array ref with default $value. If a search field already exists, the value of that field is replaced with $value.
+
<!-- Edit below this comment -->
  
  
<!-- End of Pod2Wiki -->
+
<!-- Pod2Wiki= -->
<!-- Pod2Wiki=item_get_searchfield -->==get_searchfield==
+
<!-- Pod2Wiki=head_get_satisfy_all -->
 +
===get_satisfy_all===
  
  $searchfield = $searchexp-&gt;get_searchfield( $sf_id )
+
<source lang="perl">$bool = $searchexp->get_satisfy_all
  
Return a [[API:EPrints/Search/Field|EPrints::Search::Field]] belonging to this Search with the given id.  
+
</source>
 +
Return true if this search requires that all the search fields with values are satisfied.  
  
Return undef if not searchfield of that ID belongs to this search.
+
<!-- Edit below this comment -->
  
<!-- End of Pod2Wiki -->
 
<!-- Pod2Wiki=item_clear -->==clear==
 
  
  $searchexp-&gt;clear
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_is_blank -->
 +
===is_blank===
  
Clear the search values of all search fields in the expression.
+
<source lang="perl">$boolean = $searchexp->is_blank
  
Resets satisfy_all to true.
+
</source>
 +
Return true is this searchexpression has no conditions set, otherwise true.
  
<!-- End of Pod2Wiki -->
+
If any field is set to "exact" then it can never count as unset.
<!-- Pod2Wiki=item_get_satisfy_all -->==get_satisfy_all==
 
  
  $bool = $searchexp-&gt;get_satisfy_all
+
<!-- Edit below this comment -->
  
Return true if this search requires that all the search fields with values are satisfied.
 
  
<!-- End of Pod2Wiki -->
+
<!-- Pod2Wiki= -->
<!-- Pod2Wiki=item_is_blank -->==is_blank==
+
<!-- Pod2Wiki=head_serialise -->
 +
===serialise===
  
  $boolean = $searchexp-&gt;is_blank
+
<source lang="perl">$string = $searchexp->serialise
  
Return true is this searchexpression has no conditions set, otherwise true.
+
</source>
 +
Return a text representation of the search expression, for persistent storage. Doesn't store table or the order by fields, just the field names, values, default order and satisfy_all.
  
If any field is set to "exact" then it can never count as unset.
+
<!-- Edit below this comment -->
  
<!-- End of Pod2Wiki -->
 
<!-- Pod2Wiki=item_serialise -->==serialise==
 
  
  $string = $searchexp-&gt;serialise
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_from_string -->
 +
===from_string===
  
Return a text representation of the search expression, for persistent storage. Doesn't store table or the order by fields, just the field names, values, default order and satisfy_all.
+
<source lang="perl">$searchexp->from_string( $string )
  
<!-- End of Pod2Wiki -->
+
</source>
<!-- Pod2Wiki=item_from_string -->==from_string==
+
Unserialises the contents of $string but only into the fields alrdeady existing in $searchexp. Set the order and satisfy_all mode but do not  affect the dataset or allow blank.
  
  $searchexp-&gt;from_string( $string )
+
<!-- Edit below this comment -->
  
Unserialises the contents of $string but only into the fields alrdeady existing in $searchexp. Set the order and satisfy_all mode but do not  affect the dataset or allow blank.
 
  
<!-- End of Pod2Wiki -->
+
<!-- Pod2Wiki= -->
<!-- Pod2Wiki=item_clone -->==clone==
+
<!-- Pod2Wiki=head_clone -->
 +
===clone===
  
  $newsearchexp = $searchexp-&gt;clone
+
<source lang="perl">$newsearchexp = $searchexp->clone
  
 +
</source>
 
Return a new search expression which is a duplicate of this one.
 
Return a new search expression which is a duplicate of this one.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_conditons -->==get_conditons==
 
  
  $conditions = $searchexp-&gt;get_conditons
 
  
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_get_conditons -->
 +
===get_conditons===
 +
 +
<source lang="perl">$conditions = $searchexp->get_conditons
 +
 +
</source>
 
Return a tree of [[API:EPrints/Search/Condition|EPrints::Search::Condition]] objects describing the simple steps required to perform this search.
 
Return a tree of [[API:EPrints/Search/Condition|EPrints::Search::Condition]] objects describing the simple steps required to perform this search.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_dataset -->==get_dataset==
 
  
  $dataset = $searchexp-&gt;get_dataset
 
  
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_get_dataset -->
 +
===get_dataset===
 +
 +
<source lang="perl">$dataset = $searchexp->get_dataset
 +
 +
</source>
 
Return the [[API:EPrints/DataSet|EPrints::DataSet]] which this search relates to.
 
Return the [[API:EPrints/DataSet|EPrints::DataSet]] which this search relates to.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_set_dataset -->==set_dataset==
+
 
 +
 
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_set_dataset -->
 +
===set_dataset===
  
  $searchexp-&gt;set_dataset( $dataset )
+
<source lang="perl">$searchexp->set_dataset( $dataset )
  
 +
</source>
 
Set the [[API:EPrints/DataSet|EPrints::DataSet]] which this search relates to.
 
Set the [[API:EPrints/DataSet|EPrints::DataSet]] which this search relates to.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_render_description -->==render_description==
+
 
  
  $xhtml = $searchexp-&gt;render_description
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_render_description -->
 +
===render_description===
  
 +
<source lang="perl">$xhtml = $searchexp->render_description
 +
 +
</source>
 
Return an XHTML DOM description of this search expressions current parameters.
 
Return an XHTML DOM description of this search expressions current parameters.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_render_conditions_description -->==render_conditions_description==
 
  
  $xhtml = $searchexp-&gt;render_conditions_description
 
  
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_render_conditions_description -->
 +
===render_conditions_description===
 +
 +
<source lang="perl">$xhtml = $searchexp->render_conditions_description
 +
 +
</source>
 
Return an XHTML DOM description of this search expressions conditions. ie title is "foo"  
 
Return an XHTML DOM description of this search expressions conditions. ie title is "foo"  
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_render_order_description -->==render_order_description==
+
 
 +
 
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_render_order_description -->
 +
===render_order_description===
  
  $xhtml = $searchexp-&gt;render_order_description
+
<source lang="perl">$xhtml = $searchexp->render_order_description
  
 +
</source>
 
Return an XHTML DOM description of how this search is ordered.
 
Return an XHTML DOM description of how this search is ordered.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_set_property -->==set_property==
 
  
  $searchexp-&gt;set_property( $property, $value );
 
  
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_set_property -->
 +
===set_property===
 +
 +
<source lang="perl">$searchexp->set_property( $property, $value );
 +
 +
</source>
 
Set any single property of this search, such as the order.
 
Set any single property of this search, such as the order.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_searchfields -->==get_searchfields==
+
 
 +
 
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_get_searchfields -->
 +
===get_searchfields===
  
  @search_fields = $searchexp-&gt;get_searchfields()
+
<source lang="perl">@search_fields = $searchexp->get_searchfields()
  
 +
</source>
 
Return the [[API:EPrints/Search/Field|EPrints::Search::Field]] objects relating to this search.
 
Return the [[API:EPrints/Search/Field|EPrints::Search::Field]] objects relating to this search.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_non_filter_searchfields -->==get_non_filter_searchfields==
+
 
  
  @search_fields = $searchexp-&gt;get_non_filter_searchfields();
+
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_get_non_filter_searchfields -->
 +
===get_non_filter_searchfields===
  
 +
<source lang="perl">@search_fields = $searchexp->get_non_filter_searchfields();
 +
 +
</source>
 
Return the [[API:EPrints/Search/Field|EPrints::Search::Field]] objects relating to this search, which are normal search fields, and not "filters".
 
Return the [[API:EPrints/Search/Field|EPrints::Search::Field]] objects relating to this search, which are normal search fields, and not "filters".
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_set_searchfields -->==get_set_searchfields==
 
  
  @search_fields = $searchexp-&gt;get_set_searchfields
 
  
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_get_set_searchfields -->
 +
===get_set_searchfields===
 +
 +
<source lang="perl">@search_fields = $searchexp->get_set_searchfields
 +
 +
</source>
 
Return the searchfields belonging to this search expression which have a value set.  
 
Return the searchfields belonging to this search expression which have a value set.  
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_cache_id -->==get_cache_id==
 
  
  $cache_id = $searchexp-&gt;get_cache_id
 
  
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_get_cache_id -->
 +
===get_cache_id===
 +
 +
<source lang="perl">$cache_id = $searchexp->get_cache_id
 +
 +
</source>
 
Return the ID of the cache containing the results of this search, if known.
 
Return the ID of the cache containing the results of this search, if known.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_perform_search -->==perform_search==
 
  
  $results = $searchexp-&gt;perform_search
 
  
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_perform_search -->
 +
===perform_search===
 +
 +
<source lang="perl">$results = $searchexp->perform_search
 +
 +
</source>
 
Execute this search and return a [[API:EPrints/List|EPrints::List]] object representing the results.
 
Execute this search and return a [[API:EPrints/List|EPrints::List]] object representing the results.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_ids_by_field_values -->==get_ids_by_field_values==
+
 
 +
 
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_perform_distinctby -->
 +
===perform_distinctby===
 +
 
 +
<source lang="perl">$ids_map = $searchexp->perform_distinctby( $fields )
  
  $hash = $searchexp-&gt;get_ids_by_field_values( $field )
+
</source>
 +
Perform a DISTINCT on $fields to find all unique ids by value.
  
 +
<!-- Edit below this comment -->
 +
 +
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_perform_groupby -->
 +
===perform_groupby===
 +
 +
<source lang="perl">($values, $counts) = $searchexp->perform_groupby( $field )
 +
 +
</source>
 +
Perform a SQL GROUP BY on $field based on the current search parameters.
 +
 +
Returns two array references, one containing a list of unique values and one a list of counts for each value.
 +
 +
<!-- Edit below this comment -->
 +
 +
 +
<!-- Pod2Wiki= -->
 +
<!-- Pod2Wiki=head_get_ids_by_field_values -->
 +
===get_ids_by_field_values===
 +
 +
<source lang="perl">$hash = $searchexp->get_ids_by_field_values( $field )
 +
 +
</source>
 
Find the ids for each unique value in $field.
 
Find the ids for each unique value in $field.
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=head_undocumented_methods -->=UNDOCUMENTED METHODS=
+
 
{{API:Undocumented Methods}}<!-- End of Pod2Wiki -->
+
 
<!-- Pod2Wiki=item_cache_results -->==cache_results==
+
<!-- Pod2Wiki= -->
 +
<!-- Edit below this comment -->
 +
 
  
<!-- End of Pod2Wiki -->
+
<!-- Pod2Wiki= -->
<!-- Pod2Wiki=item_count -->==count==
+
<!-- Pod2Wiki=head_copyright -->
 +
==COPYRIGHT==
 +
Copyright 2000-2011 University of Southampton.
  
<!-- End of Pod2Wiki -->
+
This file is part of EPrints http://www.eprints.org/.
<!-- Pod2Wiki=item_dispose -->==dispose==
 
  
<!-- End of Pod2Wiki -->
+
EPrints is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
<!-- Pod2Wiki=item_from_string_raw -->==from_string_raw==
 
  
<!-- End of Pod2Wiki -->
+
EPrints is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
<!-- Pod2Wiki=item_get_conditions -->==get_conditions==
 
  
<!-- End of Pod2Wiki -->
+
You should have received a copy of the GNU Lesser General Public License along with EPrints.  If not, see http://www.gnu.org/licenses/.
<!-- Pod2Wiki=item_get_ids -->==get_ids==
 
  
<!-- End of Pod2Wiki -->
+
<!-- Edit below this comment -->
<!-- Pod2Wiki=item_get_records -->==get_records==
 
  
<!-- End of Pod2Wiki -->
 
<!-- Pod2Wiki=item_map -->==map==
 
  
<!-- End of Pod2Wiki -->
+
<!-- Pod2Wiki= -->
<!-- Pod2Wiki=_postamble_ --><!-- End of Pod2Wiki -->
+
<!-- Pod2Wiki=_postamble_ -->
 +
<!-- Edit below this comment -->

Latest revision as of 08:18, 6 July 2018

EPrints 3 Reference: Directory Structure - Metadata Fields - Repository Configuration - XML Config Files - XML Export Format - EPrints data structure - Core API - Data Objects


API: Core API

Latest Source Code (3.4, 3.3) | Revision Log | Before editing this page please read Pod2Wiki


NAME

EPrints::Search - retrieve objects based on search criteria


DESCRIPTION

The Search object represents the conditions of a single search.

Executing a search returns an EPrints::List object.

A search expression can also render itself as a web-form, populate itself with values from that web-form and render the results as a web page.

EPrints::Plugin::Search provides a pluggable architecture for searching EPrints.


SYNOPSIS

Searching for Eprints

  $ds = $repo->dataset( "archive" );
  
  # NB 'archive' is an implicit filter on eprint.status
  $list = $ds->search(filters => [{
    meta_fields => [qw( eprintid )], value => 23,
  }]);
  
  $list = $ds->search(search_fields => [{
    meta_fields => [qw( creators_name )], value => "John Smith",
  }]);
  
  $searchexp = $ds->prepare_search();
  $searchexp->add_field(
    fields => [
      $ds->field('creators_name')
    ],
    value => "John Smith",
    match => "EQ", # EQuals
  );
  $searchexp->add_field(
    fields => [
      $ds->field('title')
    ],
    value => "eagle buzzard",
    match => "IN", # INdex
  );

See EPrints::DataSet for more API methods for constructing search objects.


Getting Results

  $list = $searchexp->perform_search;
  
  my $count = $list->count;
  
  my $ids = $results->ids( 0, 10 );
  my $ids = $results->ids; # Get all matching ids
  
  my $info = { matches => 0 };
  sub fn {
    my( $session, $dataset, $eprint, $info ) = @_;
    $info->{matches}++;
  };
  $list->map( \&fn, $info );

See EPrints::List for more.


METHODS

new

$searchexp = EPrints::Search->new( %params )

Create a new search expression.

The parameters are split into two parts. The general parameters and those which influence how the HTML form is rendered, and the results displayed.

GENERAL PARAMETERS

  • session (required)
The current EPrints::Session
  • dataset OR dataset_id (required)
Either the EPrints::DataSet to search, or the ID of it.
  • allow_blank (default 0)
Unless this is set, a search with no conditions will return zero records rather than all records.
  • satisfy_all (default 1)
If this is true than all search-fields much be satisfied, if false then results matching any search-field will be returned.
  • search_fields
A reference to an array of search field configuration structures. Each takes the form { id=>"...", default=>"..", meta_fields=>"..." } where the meaning is the same as for search configuration in ArchiveConfig.
Search fields can also be added to the search expression after it has been constructed.
  • order
The order the results should be returned. This is a key to the list of orders available to this dataset, defined in ArchiveConfig.pm
  • custom_order
"order" limits you to the orders specified in ArchiveConfig, and is usually used by the web page based searching. custom_order allows you to specify any order you like. The format is foo/-bar. This means that the results will be sorted by foo and then any with equal foo values will be reverse sorted by bar. More than 2 fields can be specified.
  • keep_cache
If true then the search results produced will be stored in the database even after the current script ends. This is useful for speeding up page 2 onwards of a search.
keep_cache may get set to true anyway for various reasons, but setting the parameter makes certain of it.
  • cache_id
The ID of a cached search. The cache contains both the results of the search, and the parameters used for the search.
If the cache still exists, it will set the default values of the search fields, and when the search is performed it will skip the search and build a search results object directly from the cache.
  • limit
Limit the number of matching records to limit.

WEB PAGE RELATED PARAMETERS

  • prefix (default "")
When generating the web form and reading back from the web form, the prefix is inserted before the form names of all fields. This is useful if you need to put two search expressions in a single form for some reason.
  • staff (default 0)
If true then this is a "staff" search, which prevents searching unless the user is staff, and the results link to the staff URL of an item rather than the public URL.
  • filters
A reference to an array of filter definitions.
Filter definitions take the form of: { value=>"..", match=>"..", merge=>"..", id=>".." } and work much like normal search fields except that they do not appear in the web form so force certain search parameters on the user.
An optional parameter of describe=>0 can be set to supress the filter being mentioned in the description of the search.


from_cache

$ok = $thing->from_cache( $id )

Populate this search expression with values from the given cache.

Return false if the cache does not exist.


add_field

$searchfield = $searchexp->add_field( %opts )
  fields - one or more fields to search over
  match - match type
  merge - merge type
  value - value to match against (for EX matches, NULL = is_null!)
  id - search field id, if not the name of the first field
  filter - is filter-type
  show_help - show help in search input

Adds a new search in $fields which is either a single EPrints::MetaField or a list of fields in an array ref with default $value. If a search field already exists, the value of that field is replaced with $value.

See EPrints::Search::Field for details on match/merge etc.


Note relating to the for EX matches, NULL = is_null documentation above.

To search for items with no value, use something similar to this - explicitly setting the value to undef.:

### Example: search for items in the review area with no date set.
my $ds = $session->dataset( "buffer" );
my $searchexp = $ds->prepare_search();
$searchexp->add_field(
    fields => [
      $ds->field( 'date' ),
    ],
    value => undef,
    match => "EX",
);


clear

$searchexp->clear

Clear the search values of all search fields in the expression.

Resets satisfy_all to true.


get_satisfy_all

$bool = $searchexp->get_satisfy_all

Return true if this search requires that all the search fields with values are satisfied.


is_blank

$boolean = $searchexp->is_blank

Return true is this searchexpression has no conditions set, otherwise true.

If any field is set to "exact" then it can never count as unset.


serialise

$string = $searchexp->serialise

Return a text representation of the search expression, for persistent storage. Doesn't store table or the order by fields, just the field names, values, default order and satisfy_all.


from_string

$searchexp->from_string( $string )

Unserialises the contents of $string but only into the fields alrdeady existing in $searchexp. Set the order and satisfy_all mode but do not affect the dataset or allow blank.


clone

$newsearchexp = $searchexp->clone

Return a new search expression which is a duplicate of this one.


get_conditons

$conditions = $searchexp->get_conditons

Return a tree of EPrints::Search::Condition objects describing the simple steps required to perform this search.


get_dataset

$dataset = $searchexp->get_dataset

Return the EPrints::DataSet which this search relates to.


set_dataset

$searchexp->set_dataset( $dataset )

Set the EPrints::DataSet which this search relates to.


render_description

$xhtml = $searchexp->render_description

Return an XHTML DOM description of this search expressions current parameters.


render_conditions_description

$xhtml = $searchexp->render_conditions_description

Return an XHTML DOM description of this search expressions conditions. ie title is "foo"


render_order_description

$xhtml = $searchexp->render_order_description

Return an XHTML DOM description of how this search is ordered.


set_property

$searchexp->set_property( $property, $value );

Set any single property of this search, such as the order.


get_searchfields

@search_fields = $searchexp->get_searchfields()

Return the EPrints::Search::Field objects relating to this search.


get_non_filter_searchfields

@search_fields = $searchexp->get_non_filter_searchfields();

Return the EPrints::Search::Field objects relating to this search, which are normal search fields, and not "filters".


get_set_searchfields

@search_fields = $searchexp->get_set_searchfields

Return the searchfields belonging to this search expression which have a value set.


get_cache_id

$cache_id = $searchexp->get_cache_id

Return the ID of the cache containing the results of this search, if known.


perform_search

$results = $searchexp->perform_search

Execute this search and return a EPrints::List object representing the results.


perform_distinctby

$ids_map = $searchexp->perform_distinctby( $fields )

Perform a DISTINCT on $fields to find all unique ids by value.


perform_groupby

($values, $counts) = $searchexp->perform_groupby( $field )

Perform a SQL GROUP BY on $field based on the current search parameters.

Returns two array references, one containing a list of unique values and one a list of counts for each value.


get_ids_by_field_values

$hash = $searchexp->get_ids_by_field_values( $field )

Find the ids for each unique value in $field.



COPYRIGHT

Copyright 2000-2011 University of Southampton.

This file is part of EPrints http://www.eprints.org/.

EPrints is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

EPrints is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with EPrints. If not, see http://www.gnu.org/licenses/.