Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

嵌套坐标搜索 #26

Open
jamesBan opened this issue Aug 22, 2020 · 0 comments
Open

嵌套坐标搜索 #26

jamesBan opened this issue Aug 22, 2020 · 0 comments
Labels
search 搜索引擎

Comments

@jamesBan
Copy link
Owner

创建mapping

PUT /my_locations
{
  "mappings": {
    "properties": {
      "shop_location": {
        "type": "nested",
        "properties": {
          "location": {"type":"geo_point"},
          "name": {"type": "keyword"}
        }
      }
    }
  }
}

添加 数据

PUT /my_locations/_doc/2
{
    "shop_location": [
	    {
		    "location": {"lat": 30.292755,"lon": 120.016573},
		    "name": "杭师大"
	    },
	    {
	    	"location": {"lat": 30.283565, "lon": 119.992541},
	    	"name": "绿苑"
	    },
	    {
	    	"location":{"lat": 30.289494, "lon": 119.988249},
	    	"name": "梦想小镇"
	    }
    ]
}

//1
PUT /my_locations/_doc/1
{
    "shop_location": [
        {
            "location": {"lat": 30.128427,"lon": 120.211254},
            "name": "湘湖"
        },
        {
            "location": {"lat": 30.226221, "lon": 120.143276},
            "name": "太子湾"
        },
        {
            "location":{"lat": 30.239569, "lon": 120.125595},
            "name": "茅家埠"
        }
    ]
}

//3
PUT /my_locations/_doc/3
{
    "shop_location": [
        {
            "location": {"lat": 30.217469,"lon": 120.11787},
            "name": "杨梅岭"
        },
        {
            "location": {"lat": 30.22674, "lon": 120.096326},
            "name": "法喜寺"
        },
        {
            "location":{"lat": 30.2607, "lon": 120.091434},
            "name": "汽车西站"
        }
    ]
}

//4
PUT /my_locations/_doc/4
{
    "shop_location": [
        {
          "location": {
                "lat": 30.319431,
                "lon": 120.036314
            },
            "name": "云谷国际"
        },
        {
            "location":{
                "lat": 30.291717,
                "lon": 120.212954
            },
            "name": "火车东站"
        }
    ]
}

搜索

GET /my_locations/_search
{
    "query": {
        "nested": {
            "path": "shop_location",
            "query": {
                "bool": {
                    "must": {
                        "match_all": {}
                    },
                    "filter": {
                        "geo_distance": {
                            "distance": "12km",
                            "shop_location.location": {
                                "lat": 30.289494,
                                "lon": 119.988249
                            }
                        }
                    }
                }
            }
        }
    },
    "sort": [
        {
            "_geo_distance": {
                "shop_location.location": {
                    "lat": 30.289494,
                    "lon": 119.988249
                },
                "nested":{
                    "path": "shop_location"
                },
                "order": "asc",
                "distance_type":"plane",
                "mode": "min"
            }
        }
    ]
}
@jamesBan jamesBan added the search 搜索引擎 label Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
search 搜索引擎
Projects
None yet
Development

No branches or pull requests

1 participant