datastore query 问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
timshi
V2EX    问与答

datastore query 问题

  •  
  •   timshi 2010-12-10 06:56:06 +08:00 5261 次点击
    这是一个创建于 5419 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假设我有这样一个 model

    class Person{
    @Persistent
    private List<Category> tags = ArrayList<Category>()
    }

    I want to let the user query a person based on his/her tag, so I had my query filter like this:

    tags.contains(tagValue1)

    and if the user want to search for multiple tags, I would just add to the filter so if the user is searching for 3 tags, then the query would be

    tags.contains(tagValue1) && tags.contains(tagValue2) && tags.contains(tagValue3)

    I think this approach is wrong, because the datastore then needs to have an index that have the tags property three times... and if the user search for more than 3 tags at a time then it will be broken.

    What's the proper way to do this? Do you guys have any suggestions?
    6 条回复    1970-01-01 08:00:00 +08:00
    MarkFull
        1
    MarkFull  
       2010-12-10 09:06:43 +08:00
    这个是3个queries吧
    timshi
        2
    timshi  
    OP
       2010-12-10 11:44:05 +08:00
    你是说我必须做3个query然后再intersect the result myself?
    MarkFull
        3
    MarkFull  
       2010-12-10 11:55:09 +08:00
    intersect是下策,built-in的GQL我不是很熟悉
    timshi
        4
    timshi  
    OP
       2010-12-10 12:43:49 +08:00
    那你会怎么做? 我本来以为这个个index应该是这样的

    person1, tagValue1
    person1, tagValue2
    person1, tagValue3

    i.e an additional for each value in the multi-value property, and GAE should do a merge join to get me the result.

    But my index file looks like this

    <datastore-index kind="Person" ancestor="false" source="auto">
    <property name="tags" direction="asc"/>
    <property name="tags" direction="asc"/>
    <property name="tags" direction="asc"/>
    <property name="tags" direction="asc"/>
    .....
    MarkFull
        5
    MarkFull  
       2010-12-10 12:58:25 +08:00
    no, each property should specify once for the same kind (Person).
    There should be only one line of <property name="tags" direction="asc"/>

    This is the GQL format you can reference:
    SELECT [* | __key__] FROM <kind>
    [WHERE <condition> [AND <condition> ...]]
    [ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]]
    [LIMIT [<offset>,]<count>]
    [OFFSET <offset>]

    <condition> := <property> {< | <= | > | >= | = | != } <value>
    <condition> := <property> IN <list>
    <condition> := ANCESTOR IS <entity or key>
    MarkFull
        6
    MarkFull  
       2010-12-10 13:01:30 +08:00
    Instead of using GQL, maybe you can use this:

    filter(property_operator, value)
    Adds a property condition filter to the query. Only entities with properties that meet all of the conditions will be returned by the query.
    Arguments:
    property_operator
    A string containing the property name, and an optional comparison operator. The name and the operator must be separated by a space, as in: age > The following comparison operators are supported: < <= = >= > != IN If the operator is omitted from the string (the argument is just the property name), the filter uses the = operator.
    value
    The value to use in the comparison on the right-hand side of the expression. Its type should be the value data type for the property being compared. See Types and Property Classes.
    query.filter('height >', 42).filter('city = ', 'Seattle')

    query.filter('user = ', users.get_current_user())

    from http://code.google.com/appengine/docs/python/datastore/queryclass.html#Query_filter
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5507 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 03:46 PVG 11:46 LAX 20:46 JFK 23:46
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86