Elastic Search Street Range Search

本小妞迷上赌 提交于 2020-01-06 05:14:19

问题


We have two fields Street Number 1 & Street Number 2 in Elastic Search. We also have an Address field in Elastic Search and Address is a combination of 2 fields with some other fields. So we have an address as:

  • 1604-1612 Calver Building
  • 1608- 1645 Park House

If the user is searching with 1610 both the address should be returned.

Any help on how the query can be formed?


回答1:


The idea would be to leverage the range data type and store the min and max street number in that field.

In your mapping, you'd have something like this:

PUT property_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "street_number_range": {
          "type": "integer_range"
        },
        "street_name": {
          "type": "text"
        }
      }
    }
  }
}

Then you can add your two documents like this:

PUT property_index/_doc/1
{
  "street_number_range" : { 
    "gte" : 1604,
    "lte" : 1612
  },
  "street_name": "Calver Building"
}

PUT property_index/_doc/2
{
  "street_number_range" : { 
    "gte" : 1608,
    "lte" : 1645
  },
  "street_name": "Park House"
}

Finally, your query would look like this and wold return both documents

GET property_index/_search
{
  "query" : {
    "term" : {
      "street_number_range" : {
        "value": 1610
      }
    }
  }
}

UPDATE

You can also search for a range with the following query:

GET property_index/_search
{
  "query" : {
    "range" : {
      "street_number_range" : {
        "gte": 1600,
        "lte": 1650
      }
    }
  }
}


来源:https://stackoverflow.com/questions/48716910/elastic-search-street-range-search

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