Examples of search via the api?

Hi all,

I’ve been trying to get search to work on my site, but it keeps throwing errors, which are really unhelpful as they suggest that there is a result when I’m guessing there is none.

Here’s what I am trying:
$my_itemRepresentation = $this->api()->search(‘items’, [‘title’=>‘Malaysia’])->getContent();

I’m not a php guru by any stretch, but I am wondering why that returns nothing when this does:
$itemRepresentation = $this->api()->read(‘items’, 655)->getContent();

Am I forced to use the item number?

Also, I am quite frustrated with the lack of working code examples in the documentation. There’s seems to be a lot a built in expectation of expertise that a lot of users won’t have.

1 Like

The read API method reads one single resource by its identifier. search looks for multiple. The only problem you have here is that there’s no title parameter for the search. Searches limited to a specific property use the key property, which takes a multi-level array allowing for AND/OR boolean searching.

The parameters that the search accepts, including property, are listed in the documentation. In general, the parameters are the same as are used by the advanced search form, so that can also be a convenient way of constructing a query for the search operation.

Ok. Thanks. I think I’m understanding a little more. If had used a valid property rather than a field (in this case dc:title or title) it would have worked as the ID search does.

So then the question is, how do you search by the dcterms:title? It’s not really clear, at least to me, how to search for that in the documentation. I’m guessing this is an RDF full text search?

It would really really help me if you posted an example along with any guidance. It helps me understand the technical documentation which, though thorough, is more general and abstract especially for newbies.

Thanks!

1 Like

The params array (the second argument to search() should look like this:

[
    'property' => [
        [
            'property' => 'dcterms:title',
            'text' => 'Malaysia',
            'type' => 'eq',
        ],
    ],
]

That would search for items whose dcterms:title is exactly “Malaysia”. You can use in as the type instead of eq to find matches where the titles merely contain “Malaysia”.

1 Like

Oh this is great! Thank you so much! Perfectly clear now. Would love to see more of this sprinkled throughout the documentation. It would really help me wrap my brain around all of it.

Hi!
Ok this doesn’t actually work.

Here’s my request:
$my_itemRepresentation = $this->api()->search(‘items’,
[
‘property’ => [
[
‘property’ => ‘dcterms:title’,
‘text’ => ‘Malaysia’,
‘type’ => ‘eq’,
],
],
]
)->getContent();

$all_values= $my_itemRepresentation->values();

It returns the error:
Call to a member function values() on array

I’m not sure if the search returned nothing but I’m guessing it did.

Is there something wrong with the request itself? I’m really stumped on this.

Actaully changing search to searchOne worked, so

$my_itemRepresentation = $this->api()->**searchOne**(‘items’, [ ‘property’ => [ [ ‘property’ => ‘dcterms:title’, ‘text’ => ‘Malaysia’, ‘type’ => ‘eq’, ], ], ] )->getContent();

actually worked. But why would search fail in the other example when it’s just creating a values array? What’s the difference?

Thanks!

Hi @Jovi, I think the reason you get an error in the first example is because the api is returning an array of item representations whereas in the second example the api returns just one.

If you were to loop through the array in the first example, then you’d be able to call the values() method on each item.

Ah ok. Makes sense. Thanks!

Yes, @fackrellj is right on: api->search() returns an array (even if you only got one result).

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.