Google Cloud Datastore-按种类查询和按GQL查询的结果不同

时间:2018-12-09 09:23:27

标签: google-cloud-datastore gql

我有两种查询,分别是Kind和GQL

$query = $datastore
         ->query()
         ->kind('files')
         ->filter('tags','=','texture')
         ->filter('tags','=','newspaper')
         ->start($pageCursor);

与GQL查询相比,这提供了更多结果:

$query = $datastore
         ->gqlQuery
         ("SELECT * 
           FROM files
           where tags='texture' and tags='newspaper'",
           ['allowLiterals' => true]
         );

为什么他们的结果不一样?

我的查询结果种类:https://drive.google.com/open?id=1r-Tonqs1ld4bKo0woIcR_UrMUTc1em0D

如果我使用GQL查询,只会发现4个结果

更新 您可以遵循以下主题:https://github.com/googleapis/google-cloud-php/issues/1696

1 个答案:

答案 0 :(得分:0)

在没有看到代码的情况下很难找到错误,请在接下来的问题中提供更多详细信息。

我已经使用了您的示例,并且所有工作都按预期进行。该脚本的输出为:

Query by kind retrieves 19 results Query by GQL retrieves 19 results

#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Datastore\DatastoreClient;
# Your Google Cloud Platform project ID
$projectId = 'YOUR-PROJECT-ID';

$datastore = new DatastoreClient([
    'projectId' => $projectId
]);

$titles = ['man sitting near door', 'way reading paper', 'scattered magazines', 'grayscale ', 'photography of kanji text', 'texture: vintage newspaper', 'wall floor recycle light simple', 'old newspaper texture', 'Mockup Old Paper Color  ', 'Mockup Journal.', 'Old Newspaper Texture', 'Triangle Geometric Graphic Pattern Template', 'paperboard carton surface beige plain', 'paperboard simple fiber dusty texture', 'Designer Workspace', 'craft texture brown journal note', 'Brown texture', 'Newspaper collage texture', 'Texture Newspaper'];

$tags = ['newspaper, texture, space for copy, space, cover, white, textured, antique, text, press, write, worn, backdrop, surface, old, scratch, brown, ancient, light, background, vintage, effect, letter, space for text, template, blank, rough, color, line, book, pattern, stain, paper, design, copy, dirty, grunge, copyspace, aged, retro, banner, abstract, message, print, smudge, material, grungy, wooden, wall, age, chinese, china, chinese newspaper', 
    'world, war, newspaper, texture, history, grunge, pattern, american, war, battle', 'basket, yellow building, shop, colour, wallpaper, texture, misc, wallpaper, urban, bicycle, morning, sidewalk, asian, newspaper, yellow, street, bike, man, tree, china, bycicle', 
    'man, boy, male, card, paper, decoration, woman, child, smile, torn, wall, paper, poster, texture, old, affiche wall advert wallpaper portugal wall poster erosion used print urban, newspaper', 
    'white, cloud, house, bw, black and white, pattern, light, street, silhouette, texture, calligraphy, composition, japanese, japan, newspaper, character, wall, wall paper, wall art, poster, black and white, png images', 
    'newspaper, texture, space for copy, space, cover, white, textured, antique, text, press, write, worn, backdrop, surface, old, scratch, brown, ancient, light, background, vintage, effect, letter, space for text, template, blank, rough, color, line, book, pattern, stain, paper, design, copy, dirty, grunge, copyspace, aged, retro, banner, abstract, message, print, smudge, material, grungy, wooden, wall, vintage newspaper', 
    'background, pattern, vintage, texture, paper, light, space, grunge, wall, note, flat, old paper, paper texture, desk, newspaper, recycle, natural, document, nature background, clean', 
    'newspaper, texture, space for copy, space, cover, white, textured, antique, text, press, write, worn, backdrop, surface, old, scratch, brown, ancient, light, background, vintage, effect, letter, space for text, template, blank, rough, color, line, book, pattern, stain, paper, design, copy, dirty, grunge, copyspace, aged, retro, banner, abstract, message, print, smudge, material, grungy, wooden, wall, age, old paper', 
    'newspaper, cut, paper, kraft, cardboard, mockup, old, slip, ad, advertisement, advertising, ancient, announcement, antique, backdrop, background, banner, blank, border, card, christmas, communication, design, dirty, edge, empty, grunge, illustration, information, label, letter, message, note, page, parchment, pattern, poster, red, ripped, shadow, sticker, tag, template, texture, torn, vector, vintage, wallpaper, web, white', 
    'leather, magazine, mockup, book, black, spread, page, double, brochure, blank, reading, journal, template, a4, holding, hand, newspaper, front, leaflet, person, back, binding, softcover, big, pamphlet, album, business, clear, dark, design, desk, display, document, glossy, grey, hard, hardcover, layout, ledger, mag, mock up, news, opened, paper, paperback, print, sheet, showing, soft, text, texture', 
    'newspaper, texture, space for copy, space, cover, white, textured, antique, text, press, write, worn, backdrop, surface, old, scratch, brown, ancient, light, background, vintage, effect, letter, space for text, template, blank, rough, color, line, book, pattern, stain, paper, design, copy, dirty, grunge, copyspace, aged, retro, banner, abstract, message, print, smudge, material, grungy, wooden, wall, age',
    'pattern, people, texture, template, geometric, paint, home, triangle, geometric pattern, graphic, kid, child, square, bag, person, paper texture, window, creative, newspaper, plants',
    'background, pattern, vintage, texture, paper, light, space, grunge, wall, note, flat, old paper, paper texture, desk, newspaper, recycle, natural, document, nature background, clean',
    'background, pattern, vintage, texture, paper, light, space, grunge, wall, note, flat, old paper, paper texture, desk, newspaper, recycle, natural, document, nature background, clean',
    'designer, table, workspace, computer, flat, object, workstation, desktop, design, work, office, home, desk, display, lamp, concept, cup, decor, modern, illustration, monitor, interior, inside, house, education, job, book, coffee, business, light, style, background, graphic, workplace, icons, symbol, elements, equipment, emblem, lifestyle, place, decorative, collection, room, set, organization, stylish, freelance, notepad, creative, jobs, creativity, mug, email, view, bulb, tablet, notebook, top, wood, number, texture, newspaper, think, paper, talk, bubble, mock-up, speech, item, media, template, idea, management, mock, project, mail, infographic, connect, wooden, chat, icon, content, pc, space, minimalism, vector, technology, web, screen, website, white, corporate, decorated, mouse, internet, process, clock',
    'background, pattern, vintage, texture, paper, light, space, grunge, wall, note, flat, old paper, paper texture, desk, newspaper, recycle, natural, document, nature background, clean',
    'pattern, vintage, texture, paper, light, space, grunge, wall, note, flat, old paper, paper texture, desk, newspaper, vintage pattern, recycle, natural, document, clean, floor',
    'newspaper, texture, collage, news, old, background, text, book',
    'newspaper, texture, space for copy, space, cover, white, textured, antique, text, press, write, worn, backdrop, surface, old, scratch, brown, ancient, light, background, vintage, effect, letter, space for text, template, blank, rough, color, line, book, pattern, stain, paper, design, copy, dirty, grunge, copyspace, aged, retro, banner, abstract, message, print, smudge, material, grungy, wooden, wall'];

$i = 0;
foreach ($titles as $title){
    $entity = $datastore->entity('Examples');
    $entity['title'] = $title;
    $entity['tags'] = explode(", ", $tags[$i]);
    $datastore->insert($entity);
    $i = $i + 1; 
}

sleep(1);

$query = $datastore
         ->query()
         ->kind('Examples')
         ->filter('tags','=','texture')
     ->filter('tags','=','newspaper');

$results = $datastore->runQuery($query);

$count = 0;
foreach ($results as $res) {
    $count++;
}
echo 'Query by kind retrieves ', $count, ' results', PHP_EOL ;


$query = $datastore
         ->gqlQuery
         ("SELECT * 
           FROM Examples
           where tags='texture' and tags='newspaper'",
           ['allowLiterals' => true]
         );

$results = $datastore->runQuery($query);

$count = 0;
foreach ($results as $res) {
    $count++;
}
echo 'Query by GQL retrieves ' ,$count, ' results', PHP_EOL;