full text search - Ignore leading zeros with Elasticsearch -


i trying create search bar common query "serviceorderno". "serviceorderno" not number field in database, string field. examples:

000000007 000000002 wo0000042 123456789 alltextss 000000054 000000065 000000874 

the common format integer proceeded number of zeros.

how set elasticsearch searching "65" match "000000065"? want give precedence "serviceorderno" field (which have working). here @ right now:

{    "query": {       "multi_match": {          "query": "65",          "fields": ["serviceorderno^2", "_all"],       }    } } 

one way of doing using lucene flavour regular exression query:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html

"query": {      "regexp":{         "serviceorderno": "[0]*65"      } } 

also, query string query supports small set of special characters, more limited set of regular expression characters, lucene regular expressions query this: https://www.elastic.co/guide/en/elasticsearch/reference/1.x/query-dsl-query-string-query.html

"query": {     "query_string": {        "default_field": "serviceorderno",        "query": "0*65"     } } 

these simple regular expressions, both saying match character(s) contained in brackets [0] or character 0 unlimited times *.

if have ability reindex, or haven't indexed data yet, have ability make easier on writing custom analyzer. right now, using default analyzer strings on serviceorderno field. when index "serviceorderno":"00000065" es interprets 00000065.

your custom analyzer tokenize field int both "0000065" , "65", using same regular expression. benefit of regex runs once @ index time, instead of every time run query because es search against both "0000065" , "65".

you can check out the es website documentation on analyzers.

"settings":{     "analysis": {         "filter":{            "trimzero": {                 "type":"pattern_capture",                 "patterns":"^0*([0-9]*$)"            }         },        "analyzer": {            "serviceorderno":{                "type":"custom",                "tokenizer":"standard",                "filter":"trimzero"            }         }     } }, "mappings":{     "serviceorderdto": {         "properties":{             "serviceorderno":{                 "type":"string",                 "analyzer":"serviceorderno"             }         }     } } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -