# Get field in search results

**URL:** <https://forum.getkirby.com/t/get-field-in-search-results/7271>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 19, 2017, 9:00am UTC](https://forum.getkirby.com/t/get-field-in-search-results/7271 "2017-04-19T09:00:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thguenther](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thguenther/32/16708_2.png) [@thguenther](https://forum.getkirby.com/u/thguenther)\
**Post date:** [April 19, 2017, 9:00am UTC](https://forum.getkirby.com/t/get-field-in-search-results/7271/1 "2017-04-19T09:00:07Z")

</div>

Is there any way I can access the field in which a `->search()` found the keyword?

I’m working on very complex pages with different fields, not just a single text field. My search function looks something like that:

```auto
foreach ($site->index()->search($keyword) as $result):

```

But `$result` seems to be a page object without further information about the whereabouts of the keyword. Using `strpos()` on every single possible field would probably slow things down too much.

Does anyone know a solution to that? A third-party search plugin? Overwriting the `->search()` function in the core? Or did I miss something that’s already built-in?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [April 19, 2017, 9:17am UTC](https://forum.getkirby.com/t/get-field-in-search-results/7271/2 "2017-04-19T09:17:42Z")

</div>

the search function returns a sorted collection. the pages contain additional fields for `searchHits` and `searchScore` but not which search params matched – as far as I can tell. but maybe that would make a nice feature request.

> <https://github.com/getkirby/kirby/blob/e3d5311cad778dc30a5c3aadc980925d9ddd3fe6/core/pages.php#L210>

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 19, 2017, 9:21am UTC](https://forum.getkirby.com/t/get-field-in-search-results/7271/3 "2017-04-19T09:21:58Z")

</div>

You are right, the native search method only returns a collection of pages without any further information. So it loops through each field, and as @bnomei mentioned above, it weighs results by hits and score, but nothing else.

There is the Algolia plugin as an alternative, but as far as I know, it only returns pages as well.

You could create a custom search function that loops through all fields of a page and saves that information in an array, I don’t know how much that would slow down the search though. The native search methods goes through all fields as well…

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [April 19, 2017, 9:29am UTC](https://forum.getkirby.com/t/get-field-in-search-results/7271/4 "2017-04-19T09:29:40Z")

</div>

using something like this should work (untested).

```php
    // before the loop
    $page->searchMatches = ''; // add this
    //... inside the loop
      $page->searchHits += $matches;
      $page->searchScore += $matches * $score;
      $page->searchMatches .= ltrim(',', $page->searchMatches . ',' .implode(',', $r)); // add this as well
      // maybe down below at 'full match' again, don't know. ;-)

```

now each page should have a `seachMatches` property with comma separated strings.  
one could sort match-strings by score but i leave that to the kirby team. 😬

---

<div class="post-metadata">

**Author:** ![thguenther](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thguenther/32/16708_2.png) [@thguenther](https://forum.getkirby.com/u/thguenther)\
**Post date:** [April 19, 2017, 10:44am UTC](https://forum.getkirby.com/t/get-field-in-search-results/7271/5 "2017-04-19T10:44:09Z")

</div>

Thanks a lot @bnomei and @texnixe! That definitely helped me getting started.

I think I will create a custom search function with the code you suggested. Let’s see if this works out already…

Thanks again ❤

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:50pm UTC](https://forum.getkirby.com/t/get-field-in-search-results/7271/6 "2025-02-06T16:50:08Z")

</div>


