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

PFGeoPoint - index is not created automatically #4813

Closed
ranhsd opened this issue Jun 7, 2018 · 14 comments
Closed

PFGeoPoint - index is not created automatically #4813

ranhsd opened this issue Jun 7, 2018 · 14 comments

Comments

@ranhsd
Copy link
Contributor

ranhsd commented Jun 7, 2018

Hi, As much as I know PFGeoPoint field should be auto indexed by parse but it doesn't seems to work now. I don't know if it is a bug in the latest version (2.8.2).

For this project I am using MLAB as my database. My model looks like the following:

public class TFLocation : PFObject {
    @NSManaged public var zipCode : String
    @NSManaged public var location : PFGeoPoint?
}

And I see that the class was created with the coordinates values in the dashboard

screen shot 2018-06-07 at 12 33 44

When I am trying to run a nearby query I am getting the following error log (on the server side)

screen shot 2018-06-07 at 12 34 49

I go and check my database in mLab and found that the index was not auto created

screen shot 2018-06-07 at 12 35 35

This is the first time I face such issue in parse.

Thanks !

@dplewis
Copy link
Member

dplewis commented Jun 7, 2018

How are you running nearby query? Are using query.near() or Mongo Shell?

@ranhsd
Copy link
Contributor Author

ranhsd commented Jun 7, 2018

Hi @dplewis I am using the cloud code and there I'm using the JavaScript parse SDK (withinMiles)

@dplewis
Copy link
Member

dplewis commented Jun 7, 2018

@ranhsd If you can debug here.

https://github.com/parse-community/parse-server/blob/master/src/Adapters/Storage/Mongo/MongoCollection.js#L16

It looks like your error message is different.

Can you post your logs VERBOSE=1? and which version of Mongo are you using.

@joshkopecek
Copy link

@dplewis I think the issue is that the index was not created by Parse automatically

@ranhsd Parse only creates a 2dsphere index automatically if the field is specified as a Polygon
in MongoStorageAdapter.js

  createIndexesIfNeeded(className: string, fieldName: string, type: any) {
    if (type && type.type === 'Polygon') {
      const index = {
        [fieldName]: '2dsphere'
      };
      return this.createIndex(className, index);
    }
    return Promise.resolve();
  }

If you want a 2dsphere index you need to specifically create it:

const index = {
  location: "2dsphere"
}
mySchema.addIndex('location', index)
mySchema.save()

Hope that helps!

@dplewis
Copy link
Member

dplewis commented Jun 8, 2018

@joshkopecek Auto generating an index for $geoNear has always been there. Creating an index would fix this issue. I want to see why this isn't working.

  find(query, { skip, limit, sort, keys, maxTimeMS, readPreference } = {}) {
    // Support for Full Text Search - $text
    if(keys && keys.$score) {
      delete keys.$score;
      keys.score = {$meta: 'textScore'};
    }
    return this._rawFind(query, { skip, limit, sort, keys, maxTimeMS, readPreference })
      .catch(error => {
        // Check for "no geoindex" error
        if (error.code != 17007 && !error.message.match(/unable to find index for .geoNear/)) {
          throw error;
        }
        // Figure out what key needs an index
        const key = error.message.match(/field=([A-Za-z_0-9]+) /)[1];
        if (!key) {
          throw error;
        }

        var index = {};
        index[key] = '2d';
        return this._mongoCollection.createIndex(index)
          // Retry, but just once.
          .then(() => this._rawFind(query, { skip, limit, sort, keys, maxTimeMS, readPreference }));
      });
  }

@joshkopecek
Copy link

Just tried it without an index and it worked (created an index and ran the query) for me, running v2.8.1 on localhost.

@dplewis
Copy link
Member

dplewis commented Jun 8, 2018

@joshkopecek Its not working for @ranhsd tho [insert sad face emoji]

@ranhsd
Copy link
Contributor Author

ranhsd commented Jun 8, 2018

Hi @joshkopecek and @dplewis It doesn't work for me in the latest release which is 2.8.2.
There is a chance that there is some bug in this specific release ?

Thanks!

@dplewis
Copy link
Member

dplewis commented Jun 8, 2018

@ranhsd Can you debug it? I’ll look into it this weekend.

@dplewis
Copy link
Member

dplewis commented Jun 12, 2018

@ranhsd Does it work in 2.8.1?

@ranhsd
Copy link
Contributor Author

ranhsd commented Jun 13, 2018

Hi @dplewis I didn't had the time to check it. Will do it soon. Sorry I am very busy lately...

@flovilmart
Copy link
Contributor

@ranhsd any chance it's ok now? Perhaps the index was deleted inadvertently?

@fmendoza
Copy link

I think it's related: query.near() seems not working as expected on parse-server 2.8.2-2.8.4

captura de pantalla 2019-02-21 a la s 10 50 17

captura de pantalla 2019-02-21 a la s 10 50 26

@fmendoza
Copy link

Just notice that I was sending the latitude for both lat/lng 😣 it's working now you can ignore my previous message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants