代码之家  ›  专栏  ›  技术社区  ›  Micro

谷歌搜索API-有可能做一个查询?

  •  1
  • Micro  · 技术社区  · 7 年前

    使用搜索API,是否可以执行IN查询?在哪里可以查询包含在字符串数组中的字符串参数的文档?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Dan Cornilescu    7 年前

    IN 运算符,但可以使用 OR pattern IN [word1, word2, word3] pattern IN word_list

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

    index.search(' OR '.join(word_list))
    
        2
  •  0
  •   Temu    7 年前

    有几种可能性,你可以在 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 Class its options how queries work ,例如:

    只包含字段值的。此搜索使用的字符串 搜索包含单词“rose”和“water”的文档:

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

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

    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)