Google搜索API-是否可以进行IN查询?

时间:2018-09-03 03:15:54

标签: google-app-engine google-cloud-platform

使用Search API,可以进行IN查询吗?在哪里可以查询包含在字符串数组中的带有字符串参数的文档?

2 个答案:

答案 0 :(得分:2)

Search API没有IN运算符,但是可以使用OR运算符来模拟。例如,pattern IN [word1, word2, word3]pattern IN word_list可以写为:

index.search('word1 OR word2 OR word3')

和:

index.search(' OR '.join(word_list))

答案 1 :(得分:0)

是的,可以通过文档的内容查询文档。 有几种可能性,您可以在Searching for documents by their contents中找到此示例:

def query_index():
    index = search.Index('products')
    query_string = 'product: piano OR price < 5000'

    results = index.search(query_string)

    for scored_document in results:
        print(scored_document)

您还可以找到有关Query Classits options的更多信息,以及有关how queries work的更多文档,例如:

  

最简单的查询(有时称为“全局搜索”)是字符串   仅包含字段值。此搜索使用的字符串   搜索包含单词“ rose”和“ water”的文档:

def simple_search(index):
    index.search('rose water')

如果字符串数组太大,则可以选择将category数组与字符串数组一起使用,以稍微降低成本。可以排除某些类别,这将有助于减少查询处理时间。例如:

categories=[['animal','dog','cat','fish'],['positive','good','fine','great'],['negative','horrible',disgusting','awful'],['water recreation', 'relax','holidays','spa','beach','sea']]


def query_index():
    index = search.Index('products')
    for categ in categories:
        query_string = ' OR '.join(categ)
        results = index.search(query_string)
        if len(results)>0:
            print(categ[0])
            break

    for scored_document in results:
        print(scored_document)
相关问题