我需要在pouchdb和pouchdb-find中进行不区分大小写的搜索

Gus*_*ães 0 couchdb pouchdb ionic3

我的项目运行正常,唯一的问题是搜索是否区分大小写。它可以很好地搜索子字符串,但是如果我输入“ Test”,它将忽略“ test”作为有效结果。

我正在使用pouchdb-find来使搜索更容易,并且与混浊搜索和限制/跳过分页参数更加相关。

我正在使用ion-searchbar为用户键入查询的字符串。

这是我的控制器代码节选:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是组件代码摘录。

<!-- MORE UNRELATED CODE -->
<ion-searchbar
    [(ngModel)]="search_query"
    [showCancelButton]="shoulShowCancelButton" 
    (ionInput)="onInput($event)"
    (ionCancel)="onCancel($event)">
</ion-searchbar>
<!-- MORE UNRELATED CODE -->
Run Code Online (Sandbox Code Playgroud)

Gus*_*ães 6

Javascript内置了一个regex函数,因此您只需要向regex中添加inensitive选项即可。

RegExp(<string>, "i")
Run Code Online (Sandbox Code Playgroud)

您可以在w3schools中找到正则表达式选项列表。这是完整的代码:

this.pouch_query = {
  selector: { 
    data_emissao: { $gt: null },
    descricao: { $regex: RegExp(this.search_query, "i") }
  },
  sort: [ {'data_emissao' : 'desc'} ],
  limit: 10,
  skip: 0
};
Run Code Online (Sandbox Code Playgroud)