# Finding snippet location in text array

**URL:** <https://discuss.elastic.co/t/finding-snippet-location-in-text-array/237462>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [June 17, 2020, 11:31am UTC](https://discuss.elastic.co/t/finding-snippet-location-in-text-array/237462 "2020-06-17T11:31:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coding\_martian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@coding\_martian](https://discuss.elastic.co/u/coding_martian)\
**Post date:** [June 17, 2020, 11:31am UTC](https://discuss.elastic.co/t/finding-snippet-location-in-text-array/237462/1 "2020-06-17T11:31:00Z")

</div>

I'm trying to use App Search to build a document search feature. I am storing the text of documents I want to search in an array, where each element is a string consisting of all the text of a single page. My current schema is as follows:

```auto
{​

    "title": "Alice in Wonderland",​
    "url": "alice_in_wonderland.pdf",​
    "text": ["text of page 1...",​
        "text of page 2...",...​
        "text of page 545.."]​

}​

```

App search returns results with the snippet of where the query phrase is found. However, the result doesn't return the page number of where it is found (i.e, which element of the text array). Is there a way to make this happen in App Search? Or do I have to move to regular Elasticsearch.

​

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [June 17, 2020, 3:13pm UTC](https://discuss.elastic.co/t/finding-snippet-location-in-text-array/237462/2 "2020-06-17T15:13:14Z")

</div>

Since App Search doesn't support nested fields, you'd need to flatten your data structure, so something like this:

```auto
{​
    "title": "Alice in Wonderland",​
    "url": "alice_in_wonderland.pdf",​
    "page": 1,
    "text": "text of page 1"
}​,
{​
    "title": "Alice in Wonderland",​
    "url": "alice_in_wonderland.pdf",​
    "page": 2,
    "text": "text of page 2"
}​

```

Etc.

Depending on how you want to show your search results you could then, "group" on the "title" field: [https://www.elastic.co/guide/en/app-search/current/grouping.html](https://www.elastic.co/guide/en/app-search/current/grouping.html).

---

<div class="post-metadata">

**Author:** ![coding\_martian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@coding\_martian](https://discuss.elastic.co/u/coding_martian)\
**Post date:** [June 17, 2020, 7:17pm UTC](https://discuss.elastic.co/t/finding-snippet-location-in-text-array/237462/3 "2020-06-17T19:17:23Z")

</div>

That solution works for me!

Another thing that I'm wondering about is how I can modify config in react-search-ui to set group on the title field.

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [June 18, 2020, 1:12pm UTC](https://discuss.elastic.co/t/finding-snippet-location-in-text-array/237462/4 "2020-06-18T13:12:49Z")

</div>

@coding_martian Search UI doesn't have first class support for grouping, but you can add grouping to your searches using the `beforeSearchCall ` hook. See the example here: [https://github.com/elastic/search-ui/blob/master/ADVANCED.md#api-config](https://github.com/elastic/search-ui/blob/master/ADVANCED.md#api-config).

Please note that "tip" at the top of this page: [Search API group | App Search documentation [8.11] | Elastic](https://www.elastic.co/guide/en/app-search/current/grouping.html).

> A few things to remember when using Grouping: 1. Grouping does not support [Curations](https://www.elastic.co/guide/en/app-search/current/curations.html). 2. [Facets](https://www.elastic.co/guide/en/app-search/current/facets.html) only show total results and not total number of groups. 3. [Sorting](https://www.elastic.co/guide/en/app-search/current/sort.html) and [Paging](https://www.elastic.co/guide/en/app-search/current/search.html#search-paging) require the experimental `collapse` flag to be set to `true` .

You'll probably want paging and sorting to work so you should use the 'collapse' flag:

```auto
"group": { 
  "field": "title",
  "collapse": true
}

```

> The field you are grouping on must not have multiple values. Multiple values will cause an error.

If you're using collapse, you MUST make sure that you do not have multiple values for `title` in any of your fields. So make sure you don't have data that look like this:

```auto
{​
    "title": ["Alice in Wonderland",​ "Alice Returns to Wonderland"]
}

```

One last note, since Search UI won't know what to do with the grouped results, you'll need to create a custom Result view: [Reference UI - how to modify what's displayed (React newbie) - #2 by JasonStoltz](https://discuss.elastic.co/t/reference-ui-how-to-modify-whats-displayed-react-newbie/199471/2). You'll find the "grouped" results in the "\_group" field of each record. There is an example response here: [Search API group | App Search documentation [8.11] | Elastic](https://www.elastic.co/guide/en/app-search/current/grouping.html).

Hopefully that doesn't sound too intimidating, we actually do this exact same thing on [Elastic: Search Results | Elastic](https://www.elastic.co/search).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 16, 2020, 1:23pm UTC](https://discuss.elastic.co/t/finding-snippet-location-in-text-array/237462/5 "2020-07-16T13:23:45Z")

</div>

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