Elasticsearch Map case insensitive to not_analyzed documents

后端 未结 8 1586
别那么骄傲
别那么骄傲 2020-12-08 14:46

I have a type with following mapping

PUT /testindex
{
    \"mappings\" : {
        \"products\" : {
            \"properties\" : {
                \"category         


        
相关标签:
8条回答
  • 2020-12-08 15:18

    I wish I could add a comment, but I can't. So the answer to this question is "this is not possible".

    Analyzers are composed of a single Tokenizer and zero or more TokenFilters.

    I wish I could tell you something else, but spending 4 hours researching, that's the answer. I'm in the same situation. You can't skip tokenization. It's either all on or all off.

    0 讨论(0)
  • 2020-12-08 15:20

    I think this example meets your needs:

    $ curl -XPUT localhost:9200/testindex/ -d '
    {
      "settings":{
         "index":{
            "analysis":{
               "analyzer":{
                  "analyzer_keyword":{
                     "tokenizer":"keyword",
                     "filter":"lowercase"
                  }
               }
            }
         }
      },
      "mappings":{
         "test":{
            "properties":{
               "title":{
                  "analyzer":"analyzer_keyword",
                  "type":"string"
               }
            }
         }
      }
    }'
    

    taken from here: How to setup a tokenizer in elasticsearch

    it uses both the keyword tokenizer and the lowercase filter on a string field which I believe does what you want.

    0 讨论(0)
  • 2020-12-08 15:21

    it is so simple, just create mapping as follows

    {
        "mappings" : {
            "products" : {
                "properties" : {
                    "category_name" : {
                        "type" : "string" 
                    }
                }
            }
        }
    
    }
    

    No Need of giving index if you want to work with case insensitive because the default index will be "standard" that will take care of case insensitive.

    0 讨论(0)
  • 2020-12-08 15:37

    just create your custom analyzer with keyword tokenizer and lowercase token filter.

    0 讨论(0)
  • 2020-12-08 15:37

    We could achieve case insensitive searching on non-analyzed strings using ElasticSearch scripting.

    Example Query Using Inline Scripting:

    {
        "query" : {
            "bool" : {
    
                "must" : [{
                        "query_string" : {
                            "query" : "\"apache\"",
                            "default_field" : "COLLECTOR_NAME"
                        }
                    }, {
                        "script" : {
                            "script" : "if(doc['verb'].value != null) {doc['verb'].value.equalsIgnoreCase(\"geT\")}"
                        }
                    }
                ]
    
            }
        }
    }
    

    You need to enable scripting in the elasticsearch.yml file. Using scripts in search queries could reduce your overall search performance. If you want scripts to perform better, then you should make them "native" using java plugin.

    Example Plugin Code:

    public class MyNativeScriptPlugin extends Plugin {
    
        @Override
        public String name() {
            return "Indexer scripting Plugin";
        }
    
    
        public void onModule(ScriptModule scriptModule) {
            scriptModule.registerScript("my_script", MyNativeScriptFactory.class);
    
        }
    
        public static class MyNativeScriptFactory implements NativeScriptFactory {
    
            @Override
            public ExecutableScript newScript(@Nullable Map<String, Object> params) {
                return new MyNativeScript(params);
            }
    
            @Override
            public boolean needsScores() {
                return false;
            }
        }
    
        public static class MyNativeScript extends AbstractSearchScript {
            Map<String, Object> params;
    
            MyNativeScript(Map<String, Object> params) {
                this.params = params;
            }
    
            @Override
            public Object run() {
                ScriptDocValues<?> docValue = (ScriptDocValues<?>) doc().get(params.get("key"));
                if (docValue instanceof Strings) {
                    return ((String) params.get("value")).equalsIgnoreCase(((Strings) docValue).getValue());
                }
                return false;
            }
        }
    }
    

    Example Query Using Native Script:

    {
        "query" : {
            "bool" : {
    
                "must" : [{
                        "query_string" : {
                            "query" : "\"apache\"",
                            "default_field" : "COLLECTOR_NAME"
                        }
                    }, {
                        "script" : {
                            "script" : "my_script",
                            "lang" : "native",
                            "params" : {
                                "key" : "verb",
                                "value" : "GET"
                            }
                        }
                    }
                ]
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 15:39

    If you want case insensitive queries ONLY, consider changing both your data AND your query to either of lower/upper case before you go about doing your business.

    That would mean you keep your field not_analyzed and enter data/query in only one of the cases.

    0 讨论(0)
提交回复
热议问题