Realization of geographic retrieval based on MongoDB

Source: Internet
Author: User

Using MongoDB is not a lot, remember to do "parent assistant" time, used a little. It was only when I made the "dispatch system" last year that I used MongoDB again.

Here is a brief introduction to the dispatch system, dispatch system in the Cloud foot therapy (in-line, Home foot therapy) a dedicated order delivery, improve the efficiency of the order to complete a system. Mainly when a come, will be based on the order of service items, service time and service location, quickly find the most suitable and best technician, return to the user. Due to the requirements of the special industry, the order must not be assigned to the technician directly. Instead, some of the best technicians will be screened and returned to the user to choose which technician they want to assign to.

After the project background is finished, it is the question of the choice of technical solutions. In the beginning, it was a distributed, column-oriented, open-source database based on HBase, to which the technician reported latitude and longitude. The location of the technician is then calculated by storm flow. Later, it felt HBase was overweight and not easy to maintain. At the same time, taking into account the small companies will face a problem-the cost problem. Have given up on this kind of plan. Then, just want to choose a non-relational database to save data, this time think of MongoDB. Okay, here's the data layer. Distributed architecture We use Ali's Dubbo. The reason to choose it is not much to say. First of all, it is widely used in the market. Later, the characteristics will be specifically described. Distributed control uses zookeeper, which is used by activemq across system middleware. This is still a simple to say why choose this, and do not choose to use more than the market ROCKETMQ. One of the most important reasons when used in previous work, while the application scenario for MQ is master-side longitude and latitude escalation, the message reliability is not very high, while reducing the learning cost. Speaking of here, feel too much to say, today is mainly about the application of MongoDB.

Next, write a MongoDB-based geo-search implementation!

Controller layer

Find nearby @responsebody@requestmapping (value =  "/geonearn",  method = requestmethod.get,  produces = {"Application/json;charset=utf-8"}) PUBLIC STRING TESTQUERYMONGOTOPN ()  {    carpointnearquery personquery = new carpointnearquery ();     random random = new random ();     double[]  arr = mongoutil.getrandomlocation ();     //up to 100 records      personquery.setcount (+);     //random 1km m to 10km    int distance  = random.nextint (    personquery.setdistance) (distance);     personquery.setlongitude (Arr[0]);     personquery.setlatitude (arr[1]);     return json.tojsonstring (Mongolbsservice.geonearcarpoint (Personquery));}

service layer

Public carpointnearresult geonearcarpoint (carpointnearquery carpointnearquery)  {     carpointnearresult carpointnearresult = new carpointnearresult ();     if (Carpointnearquery != null && carpointnearquery.getlongitude ()  != 0.0d && carpointnearquery.getlatitude ()  != 0.0d)  {         point point = new point (carPointNearQuery.getLongitude (),  carpointnearquery.getlatitude ());         nearquery near  = nearquery.near (point, metrics.kilometers);         Query query = new query ();         //quantity          query.limit (Carpointnearquery.getcount ()  == 0?100: Carpointnearquery.getcount ()); &NBSp;       near.query (query);         //Distance         near.maxdistance (new distance ( Carpointnearquery.getdistance ()  == 0.0d?1.0d:carpointnearquery.getdistance (),  metrics.kilometers));         near.spherical (True);         //Call DAO layer, get Data         GeoResults  Georesults = this.carpointdao.geonear (near, carpoint.class,  "CarPoint");         carpointnearquery.setcount (Georesults.getcontent (). Size ());         carpointnearquery.setdistance (Near.getmaxdistance (). GetValue ());         carpointnearresult.setcarpointnearquery (carPointNearQuery);         list georesultscontent&Nbsp;= georesults.getcontent ();         arraylist resultslist  = new arraylist ();         iterator i$ =  georesultscontent.iterator ();         while (I$.hasNext ())  {             GeoResult geoResult =  ( Georesult) I$.next ();             carpointresult  carpointresult = new carpointresult ();             carpointresult.setdistance (Georesult.getdistance (). GetValue ());             carpointresult.setcarpoint ((CarPoint) Georesult.getcontent ());             Resultslist.add (Carpointresult);          }        carpointnearresult.setcarpointlist ( Resultslist);        return carpointnearresult;     } else {        logger.error ("geonear  parameter exception");         carpointnearresult.seterrorcode (ErrorCode.PARAM_ERROR);         return null;    }}

DAO layer

Public georesults<t> geonear (Nearquery near, class<t> Clazz, String collectionname) {//use mongotemplate directly to    With the Georesults georesults = This.mongoTemplate.geoNear (near, Clazz, CollectionName); return georesults;}

The above is the use of mongodbtemplate to achieve the function of geographical search, there are many ways of the MongoDB geographical Search, specific reference to the MongoDB Chinese community, there are specific introduction.


Realization of geographic retrieval based on MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.