# Implement Advanced Search on Ionic App

**URL:** <https://forum.ionicframework.com/t/implement-advanced-search-on-ionic-app/186466>\
**Category:** Ionic Framework\
**Created:** [April 3, 2020, 6:45am UTC](https://forum.ionicframework.com/t/implement-advanced-search-on-ionic-app/186466 "2020-04-03T06:45:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZombieBest](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/zombiebest/32/131540_2.png) [@ZombieBest](https://forum.ionicframework.com/u/ZombieBest)\
**Post date:** [April 3, 2020, 6:45am UTC](https://forum.ionicframework.com/t/implement-advanced-search-on-ionic-app/186466/1 "2020-04-03T06:45:17Z")

</div>

Hello everyone!  
I start saying I’m pretty new to the framework and pretty new in web development in general.  
On my App I have a JSON array updated from API with more than 100 `question` objects, a `question` object is made like this:

```auto
{
 "question": "How do you do that?",
 "short_answer": "You should press the red button",
 "full_answer": "After you press the red button a white window will appear",
 "tags": "#red #button #tutorial"
}

```

I’m trying to implement a search bar where users can search a specific question or issue they may have, so I did it this way:

**On the HTML file:**  
`<ion-searchbar *ngIf="data_loaded" placeholder="{{ 'SEARCH_PLACEHOLDER' | translate }}" showCancelButton="never" (ionChange)="searchChange($event)" (ionClear)="searchChange($event)" (ionInput)="onSearchInput()" animated="true" debounce="500" inputmode="search" spellcheck="true" ></ion-searchbar>`

**On the .ts file:**

```auto
  searchChange($event){
    this.noResults = false;
    if($event.detail!=null && $event.detail.value!="") {
      //TODO: filter questions here
      let searchVal = $event.detail.value;

      this.filtered_questions = this.filterItems(searchVal);

      if(this.filtered_questions.length == 0) {
        this.noResults = true;
      }

    }else{
      this.filtered_questions = this.globals.all_questions;
    }
    this.showSpinner = false;
  }

  filterItems(searchTerm) {
    return this.globals.all_questions.filter(question => {
      return (question.question.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 || 
            question.short_answer.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 ||
            question.full_answer.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 || 
            question.tags.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1
      );
    });
  }

```

The `filterItems` function apply a simple filter to all the questions, returning those that contains the word entered by the user.  
This of course does not work well for my particular use-case. I could split the single word out of the phrase entered by the user, but still I think the result would be poor.

I know this is surely more an Angular issue than a Ionic issue, but I wanted to give it a try and ask here if someone has some advice regarding this, maybe using external libraries to improve the search.

Thanks!

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [April 3, 2020, 5:50pm UTC](https://forum.ionicframework.com/t/implement-advanced-search-on-ionic-app/186466/2 "2020-04-03T17:50:24Z")

</div>

One option would be to look at a fulltext indexing system such as [lunr](https://github.com/olivernn/lunr.js/).

---

<div class="post-metadata">

**Author:** ![ZombieBest](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/zombiebest/32/131540_2.png) [@ZombieBest](https://forum.ionicframework.com/u/ZombieBest)\
**Post date:** [April 3, 2020, 7:52pm UTC](https://forum.ionicframework.com/t/implement-advanced-search-on-ionic-app/186466/3 "2020-04-03T19:52:58Z")

</div>

Thanks a lot! That was exactly what I was looking for!
