简体   繁体   English

Java-Google Cloud Datastore-不相等返回“找不到匹配的索引”

[英]java - Google Cloud Datastore - Inequality returning “no matching index found”

In Android Studio , I'm trying to use Google Clood Datastore from an Android app. Android Studio中 ,我尝试通过Android应用程序使用Google Clood数据存储

With the Google example https://cloud.google.com/datastore/docs/concepts/indexes#Datastore_Indexes_and_properties I am able to add Tom (32 years old) and Lucy (30 years old) as Person and as children of the Company Acme. 使用Google示例https://cloud.google.com/datastore/docs/concepts/indexes#Datastore_Indexes_and_properties,我可以将Tom(32岁)和Lucy(30岁)添加为Person和Acme公司的孩子。 I can see these two entries are in the datastore in the Google Developers Console. 我可以看到这两个条目位于Google Developers Console的数据存储区中。

  • I can retrieve children from Company Acme if I set a query filter as below. 如果我按如下所示设置查询过滤器,则可以从Acme公司检索孩子。

     Key acmeKey = makeKey("Company", "Acme").build(); makeFilter("__key__", PropertyFilter.Operator.HAS_ANCESTOR, makeValue(acmeKey)).build())); 
    • If I want to retrieve only Person where age is equal to 32 I add to the filter above makeFilter("age", PropertyFilter.Operator.EQUAL, makeValue(32)).build() Then, I get the Person Tom. 如果我只想检索年龄等于32岁的Person,则将其添加到makeFilter("age", PropertyFilter.Operator.EQUAL, makeValue(32)).build()上方的过滤器中,然后得到Person Tom。

  • But, if, instead of Operator.EQUAL I put an inequality filter like Operator.LESS_THAN or Operator.GREATER_THAN I get the error " no matching index found ". 但是,如果不是,而是放置了不像Operator.LESS_THANOperator.GREATER_THAN这样的不相等过滤器,而不是Operator.EQUAL ,则会收到错误“ 找不到匹配的索引 ”。

I think it's a problem of indexes, but I really don't understand how can I set an index. 我认为这是索引问题,但是我真的不明白如何设置索引。 I use Android Studio and I don't use at all the App Engine. 我使用Android Studio,但完全不使用App Engine。 I just want to retrieve data as I do with MySQL with a simple WHERE age > 32 . 我只想像在WHERE age > 32 MySQL中一样检索数据。

Btw, I tried with GQL and I get exactly the exact same problem, work with EQUAL , but not with GREATER_THAN (and same in https://developers.google.com/apis-explorer ). 顺便说一句,我尝试使用GQL,但遇到了完全相同的问题,可以使用EQUAL ,但不能使用GREATER_THAN (和https://developers.google.com/apis-explorer相同)。

So, does anybody know how I can fix this problem ? 那么,有人知道我该如何解决这个问题? Thanks !! 谢谢 !!

Index configuration for Cloud Datastore is handled using the gcd.sh tool. 使用gcd.sh工具处理Cloud Datastore的索引配置。

It's easiest if you're able to run your queries against the local datastore provided by gcd.sh . 如果能够对gcd.sh提供的本地数据存储区运行查询,这是最简单的。 The tool can determine exactly which indexes you'll need, and then you can use the updateindexes command to configure them on your production app. 该工具可以准确确定所需的索引,然后可以使用updateindexes命令在生产应用程序上对其进行配置。

If not, you can also configure indexes by hand: 如果没有,您也可以手动配置索引:

  1. gcd.sh create <project> -d <project>
  2. Create a file at <project>/WEB-INF/datastore-indexes.xml with the following content: <project>/WEB-INF/datastore-indexes.xml创建一个文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?> <datastore-indexes autoGenerate="true"> <datastore-index kind="Company" ancestor="true"> <property name="age" direction="asc" /> </datastore-index> </datastore-indexes>

  1. gcd.sh updateindexes <project>

The exact content of the file will depend on the queries you want to run. 文件的确切内容将取决于您要运行的查询。 This page gives additional details on the format of the indexes file. 此页面提供有关索引文件格式的其他详细信息。

So, as Ed Davisson just said, everything is done with gcd. 因此,正如Ed Davisson所说,一切都由gcd完成。

It took me a lot of time to understand what to do, so I put my experience below, maybe it will be helpful to someone. 我花了很多时间去了解该怎么做,所以我将自己的经验总结如下,也许对某人会有帮助。 I'm working with Ubuntu , I don't know about other OS. 我正在使用Ubuntu ,但对其他操作系统一无所知。

  1. Download gcd-v1beta2-rev1-2.1.1.zip in the Downloads folder Downloads文件夹中下载gcd-v1beta2-rev1-2.1.1.zip
  2. Unzip the downloaded file 解压下载的文件
  3. In the console, go to the unziped folder cd Downloads/gcd-v1beta2-rev1-2.1.1 在控制台中,转到解压缩的文件夹cd Downloads/gcd-v1beta2-rev1-2.1.1
  4. Then, ./gcd.sh create -d myDataset myProjectName where myDataset is the project ID in Google Developers Console (it is composed with 2 random nouns and a number) 然后,./ ./gcd.sh create -d myDataset myProjectName ,其中myDatasetGoogle Developers Console中的项目ID(由2个随机名词和一个数字组成)
  5. Go to the unziped folder gcd-v1beta2-rev1-2.1.1 and you should see a folder myProjectName . 转到解压缩的文件夹gcd-v1beta2-rev1-2.1.1 ,您应该看到一个文件夹myProjectName In this myProjectName folder, in the folder WEB-INF create a file datastore-indexes.xml 在此myProjectName文件夹的WEB-INF文件夹中,创建一个文件datastore-indexes.xml。
  6. As I wanted to retrieve Person who have the ancestor Company and older than 20, I had to add the index as Ed Davisson said 因为我想找回谁拥有祖先公司与年龄比20,我不得不添加索引爱德戴维森说

     <?xml version="1.0" encoding="utf-8"?> <datastore-indexes autoGenerate="true"> <datastore-index kind="Person" ancestor="true"> <property name="age" direction="asc" /> </datastore-index> </datastore-indexes> 

  7. In your console, ./gcd.sh updateindexes myProjectName 在您的控制台中,./ ./gcd.sh updateindexes myProjectName

Now, in Google Developers Console you can see this new index under Cloud Datastore -> Index 现在,在Google Developers Console中,您可以在Cloud Datastore-> Index下看到此新索引

And finally you can retrieve Person where age > 20 最后,您可以检索年龄> 20岁的人

Key acmeKey = makeKey("Company", "Acme").build();
Query.Builder query = Query.newBuilder();
query.addKindBuilder().setName("Person");
query.setFilter(makeFilter(

    makeFilter("__key__",
        HAS_ANCESTOR,
        makeValue(acmeKey)).build(),

    makeFilter("age",
        GREATER_THAN,
        makeValue(20)).build()));

 query.addOrder(makeOrder("age", ASCENDING));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM