HashtagSell / posting-api

API for storage and retrieval of posting details for Hashtagsell
0 stars 0 forks source link

Mongoose Middleware Super Query #11

Closed that1guy closed 9 years ago

that1guy commented 9 years ago

Just combing over documentation on your Mongoose Middleware (seriously awesome stuff man!)

Today when searching for items internally in htsApp our Mongo query looks like:

PostModel.find({
                coordinates: {
                    $near: {
                        $geometry: {
                            type: "Point",
                            coordinates: [ userCurrentLongBasedOnIP , userCurrentLatBasedOnIP ]
                        },
                        $maxDistance: maxDistanceAwayFromUser,
                        $minDistance: minDistanceAwayFromUser
                    }
                },
                $or: popularCategories,
                heading: { "$regex": searchTerm, "$options": 'i' }
            }, function (err, hts_results) {
               .
               .
               do something here
               .
               .
            })

Quick breakdown of this query:

  1. Simple geospatial Mongo query around the user's latitude, longitude we got from freeGeoIP
  2. We define the how far around the user we should look (e.g. If 3Taps had to search 30 miles out to find their first 99 items then that's how far out we search our internal database so that we can merge our internal results with 3Taps response).

2a. The $maxDistance and $minDistance params are increased as the user paginates.

  1. $or popularCategories is the result of an algorithm which determined our user's searchTerm, "toolboxes", is mostly found in the STOO and VPAR categories.
  2. Today I'm performing a very weak $regex string match (case insensitive) of the user's searchTerm against the heading field. I was forced to do a $regex instead of using Mongo's $text functionality because you cannot combine $text and $near in the same query (see my Stackoverflow: http://stackoverflow.com/questions/25922965/mongodb-text-with-near).

I thought this breakdown might help determine our goal in leveraging your Mongoose Middleware to formulate a geospatial, Elasticsearch-capable, category-intelligent super query! :)

Cheers man!

brozeph commented 9 years ago

Working on a geo element to the query string now... it looks like the following:

https://localhost:4043/v1/postings?geo[min]=0&geo[max]=5000&geo[coords]=-122.30257,47.44376

Where:

In the query results, the geo values appear as follows:

"options": {
    "count": 1000,
    "filters": {},
    "geo": {
      "min": "0",
      "max": "1000",
      "coords": "-122.30257,47.44376"
    },
    "sort": {},
    "start": 0
}
brozeph commented 9 years ago

Implemented a migration to insure index is correct type:

bash init/apply-migrations.sh
brozeph commented 9 years ago

https://localhost:4043/v1/postings?geo[min]=0&geo[max]=5000&geo[coords]=122.3750,37.6189

All postings within 5000 meters of SF

brozeph commented 9 years ago

https://localhost:4043/v1/postings?geo[min]=0&geo[max]=1000&geo[coords]=-122.30257,47.44376&filters[mandatory][contains][heading]=wallet

All postings within 1000 meters of the Seattle airport with a heading that contains the word "wallet"

brozeph commented 9 years ago

Verified - closing

that1guy commented 9 years ago

I'm gonna leave this ticket open as it's giving a pretty large definition of query functionality we're aiming for. Let me know if this ticket too large and I can break into smaller tickets.

Just an example of what I'm thinking.. totally up to you.

    var params = {
        filters: {
            mandatory: {
                fuzzySearch: {
                    heading: "bike"
                }
            }
        },
        geo: {
           min: {0},
           max: {1000},
           coords: {[
               long,
               lat
           ]}
        },
        sort: {
            distance: {[
               long,
               lat
           ]}
        }
    };

    $http({
        method: 'GET',
        url: ENV.postingAPI + utilsFactory.bracketNotationURL(params)
    }).then(function (response) {
        console.log('here it is', response);
        deferred.resolve(response);
    }, function (err) {
        deferred.reject(err);
    });
brozeph commented 9 years ago

Sort by distance along with distance in meters is complete... when querying by geo, distance appears as an additional field under "geo"

brozeph commented 9 years ago

Example search across multiple categories:

https://localhost:4043/v1/postings?filters[optional][exact][categoryCode]=SAPP,SANT

that1guy commented 9 years ago

31 kinda related. See question.

that1guy commented 9 years ago

This enhancement has been shipped. Built new tickets detailing small bugs within this enhancement. Closing.