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

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

我的项目运行完美,唯一的问题是搜索区分大小写。它可以很好地搜索子字符串,但如果我键入 "Test",它会忽略 "test" 作为有效结果。

我正在使用 pouchdb-find 使搜索更容易,并且与 cloudant 搜索更相关,并使用 limit/skip 参数进行分页。

我正在使用 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();
    }
}

这里是组件代码摘录。

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

Javascript 内置了一个正则表达式函数,所以你只需要在下面的正则表达式中添加激励选项即可

RegExp(<string>, "i")

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

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

您可能希望使用针对 "free text" 匹配的 pouchdb-quick-search 插件,而不是使用 pouchdb-find 系统获得的 "does this field equal this field"。

自由文本搜索索引是用源文本的预处理形式构建的——通常词尾被词干化(删除),大小写被忽略,停用词(a、和、the)被删除。查询更加流畅,"best match" 在列表顶部返回

pouchdb-quick-search 更接近您提到的 Cloudant Search(它又基于 Apache Lucene 库)。您可以在 PouchDB 中同时使用这两种类型的查询:用于确定性查询的 pouchdb-find 和用于自由文本匹配的 pouchdb-quick-search。