API:EPrints/Search

From EPrints Documentation
Revision as of 08:17, 6 July 2018 by Libjlrs (talk | contribs) (add_field)
Jump to: navigation, search

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.:

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/.