I need to make case insensitive searchs in pouchdb and pouchdb-find
Asked Answered
N

2

3

My project is working flawlessly, the only problem that I am having is that the search is case sensistive. It can search substrings just fine but if I type "Test", it ignores "test" as a valid result.

I am using pouchdb-find to make the search easier and more related to the cloudant search and limit/skip paramater for pagination.

I am using the ion-searchbar for the user to type the queried string.

Here is my controlers code's excerpt:

@Component({
    selector: 'page-notas',
    templateUrl: 'notas.html'
})
export class NotasPage {
    notas: Array<Object> = [];
    zone: any = new NgZone({ enableLongStackTrace: false });
    db: any = new PouchDB('banco_de_dados.bd');
    db_limit = 10;

    pouch_query: object = {
        selector: { data_emissao: { $gt: null } },
        sort: [ {'data_emissao' : 'desc'} ],
        limit: 10,
        skip: 0,
    };

    constructor(
        private scanner: BarcodeScanner,
        private toastCtrl: ToastController,
        private googleAnalytics: GoogleAnalytics,
        public navCtrl: NavController,
        public alertCtrl: AlertController,
        public modalCtrl: ModalController
    ) {
        this.notas = [];
    }
    //...
    // unrelated code in here
    //...
    onInput($event:any) {
        this.googleAnalytics.trackEvent('SearchBar', 'onInput', 'Event: ' + $event);
        //Here is the query options, it's working, the only problem is that it's case sensitive
        this.pouch_query = {
            selector: { 
            data_emissao: { $gt: null },
            descricao: { $regex: this.search_query }
            },
            sort: [ {'data_emissao' : 'desc'} ],
            limit: 10,
            skip: 0
        };
        // this function is a little bigger
        // butit just makes the search and list it in a ion-list
        this.refresh();
    }
}

And here is the component code excerpt.

<!-- MORE UNRELATED CODE -->
<ion-searchbar
    [(ngModel)]="search_query"
    [showCancelButton]="shoulShowCancelButton" 
    (ionInput)="onInput($event)"
    (ionCancel)="onCancel($event)">
</ion-searchbar>
<!-- MORE UNRELATED CODE -->
Needlecraft answered 12/9, 2017 at 20:33 Comment(0)
N
11

Javascript has a regex function built-in it, so you just have to add the incensitive option to the regexas follows

RegExp(<string>, "i")

You can find a list of regex options in w3schools. Here is the complete code:

this.pouch_query = {
  selector: { 
    data_emissao: { $gt: null },
    descricao: { $regex: RegExp(this.search_query, "i") }
  },
  sort: [ {'data_emissao' : 'desc'} ],
  limit: 10,
  skip: 0
};
Needlecraft answered 21/9, 2017 at 20:27 Comment(0)
C
4

You may wish to use the pouchdb-quick-search plugin which is geared towards "free text" matching rather than the "does this field equal this field" you get with the pouchdb-find system.

Free-text search indexes are constructed with a pre-processed form of the source text - usually word endings are stemmed (removed), case is ignored and stop words (a, and, the) are removed. Querying is much more fluid and the "best match" is returned at the top of the list

The pouchdb-quick-search is much closer to the Cloudant Search you mention (which in turn is based on the Apache Lucene library). You may use both types of querying at the same time with PouchDB: pouchdb-find for deterministic queries and pouchdb-quick-search for free-text matching.

Capers answered 21/9, 2017 at 21:4 Comment(3)
Hard to find comments comparing -find and -quick-search on the internet. Really glad to read that. Thanks !Pipit
didn't know about -quick-search, glad I stumble on this answer.Homebody
however quick-search seems to be largely abandoned.. which is a pity since it looks rather good..Cystitis

© 2022 - 2024 — McMap. All rights reserved.