Redis’s GEO combat

  redis

Order

This article mainly studies the use of redis’s GEO

Relevant command

geoadd

The time complexity is O(log(N))

geoadd cityGeo 116.405285 39.904989 "北京"
geoadd cityGeo 121.472644 31.231706 "上海"
  • Add latitude and longitude information

geopos

The time complexity is O(log(N))

127.0.0.1:6379> geopos cityGeo 北京
1) 1) "116.40528291463851929"
   2) "39.9049884229125027"
  • Find the longitude and latitude information of the specified key, you can specify multiple keys and return them in batch.

geodist

The time complexity is O(log(N))

127.0.0.1:6379> geodist cityGeo 北京 上海
"1067597.9668"
127.0.0.1:6379> geodist cityGeo 北京 上海 km
"1067.5980"
  • Returns the distance between two places, specifying units such as meters m, kilometers km, miles mi, feet ft

georadius

The time complexity is O(N+log(M)), n is the number of elements within the specified radius, and m is the number to be returned

georadius cityGeo 116.405285 39.904989 100 km WITHDIST WITHCOORD ASC COUNT 5
  • According to the given latitude and longitude, elements with radius not exceeding the specified distance are returned.
  • You can specify WITHDIST to return distance, WITHCOORD to return longitude and latitude, and WITHHASH to return geohash value.
  • You can specify ASC or DESC, sorted by distance
  • You can specify COUNT to limit the number of records returned

georadiusbymember

The time complexity is O(log(N)+M), n is the number of elements within the specified radius, and m is the number to be returned

georadiusbymember cityGeo 北京 100 km WITHDIST WITHCOORD ASC COUNT 5
  • Query the location of radius within the specified range according to the specified location.
  • You can specify WITHDIST to return distance, WITHCOORD to return longitude and latitude, and WITHHASH to return geohash value.
  • You can specify ASC or DESC, sorted by distance
  • You can specify COUNT to limit the number of records returned

geohash

The time complexity of finding a location is O(log(N))

127.0.0.1:6379> geohash cityGeo 北京
1) "wx4g0b7xrt0"
  • Geohash value is returned.

GEO usage instance of RedisTemplate

    @Test
    public void testAdd(){
        Long addedNum = redisTemplate.opsForGeo()
                .add(cityGeoKey,new Point(116.405285,39.904989),"北京");
        System.out.println(addedNum);
    }

    @Test
    public void testGeoGet(){
        List<Point> points = redisTemplate.opsForGeo().position(cityGeoKey,"北京","上海","深圳");
        System.out.println(points);
    }

    @Test
    public void testDist(){
        Distance distance = redisTemplate.opsForGeo()
                .distance(cityGeoKey,"北京","上海", RedisGeoCommands.DistanceUnit.KILOMETERS);
        System.out.println(distance);
    }

    @Test
    public void testNearByXY(){
        //longitude,latitude
        Circle circle = new Circle(116.405285,39.904989, Metrics.KILOMETERS.getMultiplier());
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);
        GeoResults<RedisGeoCommands.GeoLocation<String>>  results = redisTemplate.opsForGeo()
                .radius(cityGeoKey,circle,args);
        System.out.println(results);
    }

    @Test
    public void testNearByPlace(){
        Distance distance = new Distance(5,Metrics.KILOMETERS);
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);
        GeoResults<RedisGeoCommands.GeoLocation<String>>  results = redisTemplate.opsForGeo()
                .radius(cityGeoKey,"北京",distance,args);
        System.out.println(results);
    }

    @Test
    public void testGeoHash(){
        List<String> results = redisTemplate.opsForGeo()
                .hash(cityGeoKey,"北京","上海","深圳");
        System.out.println(results);
    }
  • The above shows geoadd, geopos, geodist, georadius, georadiusbymember, geohash operations using RedisTemplate respectively.
  • The attribute value of Point, the X axis is longitude Longitude, and the Y axis is latitude Latitude

Summary

Redis provides GEO with rich operations. RedisTemplate also encapsulates the corresponding api, which is very convenient to use.

doc