Elasticsearch “ends with” word in phrases


(ganeshbabu) #1

Hi All,

I try to implement a query from my content with Elasticsearch 2.4
What I need, is displaying all results which ends with e.g. 154, 024... etc.

I've tried:

GET rd_product_lpv1/_search
{
"query": {
"match_phrase_prefix": {
"STD_EXTRN_CODE.STD_EXTRN_CODE_ENDS": {
"query": "154",
"max_expansions": 5
}
}
}
}

The query mentioned above display results, where within the value begins with "154"
Example:

"STD_EXTRN_CODE": "15400007118021",

I want to display results where the value ends with 154
sample code:-
"STD_EXTRN_CODE": "02100007118154",
"STD_EXTRN_CODE": "02101231232024",

Here the settings & mappings I use:

PUT /rd_product_lpv1
{
   "settings": {
      "analysis": {
         "analyzer": {
            "analyzer_endswith":{
                "tokenizer": "keyword",
                "type": "standard",
                "filter" : [
                    "lowercase"
                ]
            }               
         }
     }
   },
   "mappings": {
    "properties": {
        "STD_EXTRN_CODE": {
          "type": "string",
           "fields": {
             "STD_EXTRN_CODE_RAW": {
                "type": "string",
                "index": "not_analyzed",
                "norms":{
                    "enabled": false
                },
                "fielddata":{
                    "format": "disabled"
                }
            },
            "STD_EXTRN_CODE_CONTAINS": {
                "type": "string",
                "analyzer": "str_index_analyzer",
                "fielddata":{
                    "format": "disabled"
                }
              },
            "STD_EXTRN_CODE_ENDS": {
                "type": "string",
                "analyzer": "analyzer_endswith",
                "fielddata":{
                    "format": "disabled"
                }
            }
          }
        }
    }
  }
}

Here the sample data,

POST rd_product_lpv1/product_lpv/1
{
  "STD_EXTRN_CODE": "15400007118021"
}

POST rd_product_lpv1/product_lpv/1
{
  "STD_EXTRN_CODE": "02100007118154"
}

Please let me know your thoughts

Thanks,
Ganeshbabu R


(Christoph) #2

Hi,

what I've seen people do in slightly similar cases is reverse the token that they want to search the suffix for using a reverse token filter. Then you can use the prefix search on those reversed fields and it will behave like suffix search. Be aware that you need to use the same analyzer at search time, but by default queries will use the analyzer defined in the field mapping, so you should be okay if you don't specify an explicit search_analyzer.


(ganeshbabu) #3

Hi @cbuescher

Thanks for the clarification..

I modified the analyzer with the reverse token filter,

     "analyzer": {
        "analyzer_endswith":{
            "tokenizer": "keyword",
            "type": "standard",
            "filter" : [
                "lowercase",
                "reverse"
            ]
        } 

Its worked as expected !!

Thanks,
Ganeshbabu R


(system) #4