Creating an auto complete search using angularfire

2020-03-24 02:24发布

问题:

I'm trying to build a simple search function for my web app. there is documentation on how to create it with real time database.

What changes do I need to make to make this work on firestore ?

this tutorial was taken from here https://angularfirebase.com/lessons/autocomplete-search-with-angular4-and-firebase/

it has a nice video as well :)

this is how to make it with real time database:

#movies.service.ts
import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from ' 
angularfire2/database';
@Injectable()
export class MoviesService {
  constructor(private db: AngularFireDatabase) { }
  getMovies(start, end): FirebaseListObservable<any> {
    return this.db.list('/movies', {
      query: {
        orderByChild: 'Title',
        limitToFirst: 10,
        startAt: start,
        endAt: end
      }
    });
  }
}

The Autocomplete Search Component

 <h1>Movie Search</h1>
 <input type="text" (keydown)="search($event)" placeholder="search 
  movies..." class="input">
  <div *ngFor="let movie of movies">
    <h4>{{movie?.Title}}</h4>
    <p>
     {{movie?.Plot}}
   </p>
 </div>
<div *ngIf="movies?.length < 1">
  <hr>
  <p>
    No results found :(
  </p>
</div>

TS

import { Component, OnInit } from '@angular/core';
import { MoviesService } from '../movies.service';
import { Subject } from 'rxjs/Subject'
@Component({
  selector: 'movie-search',
  templateUrl: './movie-search.component.html',
  styleUrls: ['./movie-search.component.scss']
})
export class MovieSearchComponent implements OnInit {
  movies;
  startAt = new Subject()
  endAt = new Subject()
  constructor(private moviesSvc: MoviesService) { }
  ngOnInit() {
    this.moviesSvc.getMovies(this.startAt, this.endAt)
                  .subscribe(movies => this.movies = movies)
  }
  search($event) {
      let q = $event.target.value
      this.startAt.next(q)
      this.endAt.next(q+"\uf8ff")
  }
}

回答1:

It is pretty much the same with Firestore.

Here the working and tested solution i just wrote :

search.ts

import { Component } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore';

@Component({
  selector: 'search',
  templateUrl: 'search.html'
})
export class SearchComponent {

  searchValue: string = "";
  results: any;

  constructor(public afs: AngularFirestore) {
  }

  search() {
    let self = this;
    self.results = self.afs.collection(`users`, ref => ref
      .orderBy("username")
      .startAt(self.searchValue.toLowerCase())
      .endAt(self.searchValue.toLowerCase()+"\uf8ff")
      .limit(10))
      .valueChanges();
  }

}

search.html

<div>
  <input type="text" (keyup)="search()" [(ngModel)]="searchValue">
  <div class="search-results">
    <div class="search-result" *ngFor="let result of results | async">
      {{ result.username }}
    </div>
  </div>
</div>

If you need a more complex search, you could use Algolia :

To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia. Consider a note-taking app where each note is a document:

You'll find more information on the official firestore documentation



回答2:

The problem you will face with Firestore is a latency one. Unsure whether this is because it is still in beta, but the latency for reads is worryingly high.