全部產品
Search
文件中心

:巢狀型別查詢

更新時間:Sep 13, 2024

NestedQuery用於查詢巢狀型別欄位中子行的資料。巢狀型別不能直接查詢,需要通過NestedQuery封裝,NestedQuery中需要指定巢狀型別欄位的路徑和一個子查詢,其中子查詢可以是任意Query類型。

前提條件

參數

參數

說明

path

路徑名,巢狀型別的列的樹狀路徑。例如news.title表示巢狀型別的news列中的title子列。

query

巢狀型別的列中子列上的查詢,子列上的查詢可以是任意Query類型。

score_mode

當列存在多個值時基於哪個值計算分數。

table_name

資料表名稱。

index_name

多元索引名稱。

inner_hits

巢狀型別欄位的子列的配置參數。包括如下配置項:

  • sort:Nested子列返回時的定序。

  • offset:當Nested列包含多個子行時,子行返回的起始位置。

  • limit:當Nested列包含多個子行時,返回子行的數量。預設值為3。

  • highlight:Nested子列高亮參數配置。具體參數配置說明請參見查詢高亮

樣本

單層級巢狀型別查詢樣本

以下樣本用於查詢表中col_nested.col_long列的值大於等於100且小於等於300的資料。

  • 5.2.1及之後版本

    使用5.2.1及之後的SDK版本時,預設的返回結果為SearchResponse對象,請求樣本如下:

    nested_query = RangeQuery('col_nested.col_long', range_from=100, range_to=300, include_lower=True, include_upper=True)
    query = NestedQuery('col_nested', nested_query)
    search_response = client.search(
        '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    )
    print('request_id : %s' % search_response.request_id)
    print('is_all_succeed : %s' % search_response.is_all_succeed)
    print('total_count : %s' % search_response.total_count)
    print('rows : %s' % search_response.rows)

    如果需要返回Tuple類型結果,您可以使用如下請求樣本實現。

    nested_query = RangeQuery('col_nested.col_long', range_from=100, range_to=300, include_lower=True, include_upper=True)
    query = NestedQuery('col_nested', nested_query)
    rows, next_token, total_count, is_all_succeed, agg_result, group_by_results = client.search(
        '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    ).v1_response()
  • 5.2.1之前版本

    使用5.2.1之前的SDK版本時,預設的返回結果為Tuple類型,請求樣本如下:

    nested_query = RangeQuery('col_nested.col_long', range_from=100, range_to=300, include_lower=True, include_upper=True)
    query = NestedQuery('col_nested', nested_query)
    rows, next_token, total_count, is_all_succeed = client.search(
        '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    )

巢狀型別查詢使用查詢高亮樣本

以下樣本用於使用NestedQuery功能查詢表中col_nested巢狀型別欄位中col_text子列的值能夠匹配tablestore的資料,並在返回結果中對關鍵詞進行高亮顯示。

def _print_rows(request_id, rows, total_count):
    print('Request ID:%s' % request_id)

    for row in rows:
        print(row)

    print('Rows return: %d' % len(rows))
    print('Total count: %d' % total_count)


def _print_search_hit(hits):
    for search_hit in hits:
        print('\t score is %.6f' % search_hit.score)
        for highlight_field in search_hit.highlight_result.highlight_fields:
            print('\t\t highlight:%s:%s' % (highlight_field.field_name, highlight_field.field_fragments))
        for inner_result in search_hit.search_inner_hits:
            print('\t\t path:%s' % (inner_result.path))
            _print_search_hit(inner_result.search_hits)


def highlight_query_for_nested(client):
    print('********** Begin HighlightQueryForNested **********')

    sort = Sort(
        sorters=[FieldSort('col_nested.col_long', sort_order=SortOrder.ASC)]
    )

    highlight_parameter = HighlightParameter("col_nested.col_text", 1, 18, '<b>', '</b>', HighlightFragmentOrder.TEXT_SEQUENCE)
    highlight_clause = Highlight([highlight_parameter], HighlightEncoder.PLAIN_MODE)

    inner_hits_parameter = InnerHits(None, 0, 10, highlight_clause)
    query = NestedQuery('n', MatchQuery('col_nested.col_text', 'tablestore'), ScoreMode.AVG, inner_hits_parameter)

    search_response = client.search('<TABLE_NAME>', '<SEARCH_INDEX_NAME>',
                                    SearchQuery(query, limit=2, get_total_count=True),
                                    ColumnsToGet(return_type=ColumnReturnType.ALL_FROM_INDEX)
                                    )

    print('----- Print Rows:')
    print('search rows count:%d' % len(search_response.rows))
    _print_rows(search_response.request_id,search_response.rows,search_response.total_count)

    print('----- Print Highlight Result:')
    search_hits = search_response.search_hits
    print('search hit count:%d' % len(search_hits))

    _print_search_hit(search_hits)

    print('********** End HighlightQuery **********')

常見問題

相關文檔