elasticlunr.js icon indicating copy to clipboard operation
elasticlunr.js copied to clipboard

Wildcard Search

Open dveuve opened this issue 6 years ago • 10 comments

I'm happily using elasticlunr.js, but I don't see any examples or documentation around supporting wildcards, which my users are requesting. It looks like this is available in lunr.js but I would rather not switch -- is there any way to deliver it in elasticlunr.js that I am missing?

dveuve avatar Apr 20 '18 13:04 dveuve

@dveuve , thanks very much for using elasticlunr.js.

Do you mean wildcars like this "*ello", ".ello"?

Elasticlunr.js not support it, and actually I don't have time to support it, maybe when I have time I could support it or if you could contribute it that would be great.

weixsong avatar Apr 23 '18 08:04 weixsong

Would like to have it, too.

wehrstedt avatar Jun 06 '18 07:06 wehrstedt

@weixsong is it possible to use wildcard for the fields as well ?, so

var index = elasticlunr(function () {
    this.addField('title.*'); // title.en, title.fr, title.etc..
    this.addField('body.*');
    this.setRef('id');
});

ctf0 avatar Jun 21 '18 04:06 ctf0

I'm not quite sure what you want to achieve with this while index-creation @ctf0

const languages = ['en', 'it', 'de', 'sp'];
languages.forEach(lang => {
    this.addField('title.' + lang);
});

You could set up a Configuration for each language and add it as parameter to the 'search'-method, if you want to react on different language depending queries

ToLuSt avatar Jun 26 '18 09:06 ToLuSt

@ToLuSt do u have an example or a link for me to read more about this ?

i've read both

  • https://github.com/weixsong/elasticlunr.js#other-languages-example-in-browser
  • https://github.com/weixsong/lunr-languages

but not sure how the multi locale search/index works

ctf0 avatar Jun 26 '18 16:06 ctf0

So your goal is to retrieve information on many different languages and you have docs which are either in english, spanish or german aso..

i didn't work with multi languages yet, but I tried it in my application and indexing is working how it should. Don't know if it is what u are looking for:

const elasticlunr = require('elasticlunr');
require('lunr-languages/lunr.stemmer.support.js')(elasticlunr);
require('lunr-languages/lunr.multi.js')(elasticlunr)
require('lunr-languages/lunr.de.js')(elasticlunr);
require('lunr-languages/lunr.es.js')(elasticlunr);

const index = elasticlunr(function () {
    this.use(elasticlunr.multiLanguage('en', 'de', 'es'));
    
    const languages = ['en', 'it', 'de', 'sp'];
    languages.forEach(lang => { // could use this to create language specific fields
        this.addField('title.' + lang);
    });
    this.setRef('id');
});

So the language specific Stemmer is used to create the index.

It's your choice to use language specific fields or not. You are also free to search on only those fields which language is currently used. (if your app knows which language is currently used)

ToLuSt avatar Jun 27 '18 09:06 ToLuSt

yeah thats exactly what am after, for the search part am still not sure how, the docs doesnt have this part.

ctf0 avatar Jun 27 '18 11:06 ctf0

Maybe s.th. like this for individual configs.

    /**
     * Configure search config
     */
    searchConfig(language: string): any {
        let userConfig;
    
        // Choose Config depending on chosen language
        if (language === 'en') {
           userConfig = {
                fields: {
                    title.en: { boost: 2 }
                },
                bool: 'OR',
                expand: true
            };
        } else if () { 
        } else { // All Languages
            userConfig = {
                fields: {
                    title.en: { boost: 2 },
                    title.sp: { boost: 2 }
                },
                bool: 'OR',
                expand: true
            };
        }
        let configStr = null;
        if (userConfig != null) {
            configStr = JSON.stringify(userConfig);
        }
        const config = new elasticlunr.Configuration(configStr, this.fields).get();

        return config; // add this to search method
    }

ToLuSt avatar Jun 29 '18 07:06 ToLuSt

maybe it could be as simple as this ?

index.search("Oracle database profit", {
    fields: {
        title[this.currentLocale]: {boost: 2},
        body[this.currentLocale]: {boost: 1}
    }
});

ctf0 avatar Jun 29 '18 12:06 ctf0

So your goal is to retrieve information on many different languages and you have docs which are either in english, spanish or german aso..

i didn't work with multi languages yet, but I tried it in my application and indexing is working how it should. Don't know if it is what u are looking for:

const elasticlunr = require('elasticlunr');
require('lunr-languages/lunr.stemmer.support.js')(elasticlunr);
require('lunr-languages/lunr.multi.js')(elasticlunr)
require('lunr-languages/lunr.de.js')(elasticlunr);
require('lunr-languages/lunr.es.js')(elasticlunr);

const index = elasticlunr(function () {
    this.use(elasticlunr.multiLanguage('en', 'de', 'es'));
    
    const languages = ['en', 'it', 'de', 'sp'];
    languages.forEach(lang => { // could use this to create language specific fields
        this.addField('title.' + lang);
    });
    this.setRef('id');
});

So the language specific Stemmer is used to create the index.

It's your choice to use language specific fields or not. You are also free to search on only those fields which language is currently used. (if your app knows which language is currently used)

@ToLuSt it shows me

Uncaught TypeError: elasticlunr.multiLanguage is not a function

robertIsaac avatar Jul 15 '22 13:07 robertIsaac