Range query with elasticsearch for string

邮差的信 提交于 2019-12-09 07:21:21

问题


I am trying use range query with elasticsearch

{
    "query": {
        "range": {
             "order_no": {
                 "gte": "VM-0001",
                 "lte": "VM-0005"
             }
         }
    }
}

But elastic return no result. I found system have problem with string include - or _

This is mapping of that field:

"order_no" : {
      "type" : "string",
      "index_analyzer" : "str_index_analyzer",
      "search_analyzer" : "str_search_analyzer"
}

{
  "analysis": {
    "analyzer": {
      "str_search_analyzer": {
        "tokenizer": "keyword",
        "filter": [
          "lowercase"
        ]
      },
      "str_index_analyzer": {
        "tokenizer": "keyword",
        "filter": [
          "lowercase",
          "substring"
        ]
      }
    },
    "filter": {
      "substring": {
        "type": "nGram",
        "min_gram": 1,
        "max_gram": 20
      }
    }
  }
}

回答1:


{
  "query": {
    "range": {
      "order_no": {
        "gte": "vm-0001",
        "lte": "vm-0005"
      }
    }
  }
}



回答2:


As per the documentation, in case of string fields, Elasticsearch uses a TermRangeQuery which as far as I know doesn't analyze the term to search for. This means that your range VM-0001 - VM-0005 searches for exact these terms. Whereas you have in your index something like vm-0001 (lowercase). So, either use:

{
  "query": {
    "range": {
      "order_no": {
        "gte": "vm-0001",
        "lte": "vm-0005"
      }
    }
  }
}

or add another field in your index where you keep the order_no as keyword without any lowercasing or nGram-atization.



来源:https://stackoverflow.com/questions/27144102/range-query-with-elasticsearch-for-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!