Tablestore SDK for Java の Exists クエリを使用して、指定したフィールドが存在するかどうかに基づいてデータをフィルタリングできます。
前提条件
Tablestore SDK for Java をインストールし、クライアントを初期化します。
機能の説明
存在クエリ (ExistsQuery、NULL クエリまたは NULL 値クエリとも呼ばれる) は、指定されたインデックス化フィールドが存在する行に一致します。対応する列がデータテーブルに書き込まれていない場合、そのフィールドは存在しないと見なされます。空の配列も、存在しない配列フィールドとして扱われます。
search を呼び出すとき、クエリタイプを ExistsQuery に設定し、フィールド名を指定します。Nested の親フィールドまたはサブフィールドをクエリするには、ExistsQuery を NestedQuery でラップします。フィールドが存在しない行に一致させるには、ExistsQuery を BoolQuery.mustNotQueries に追加します。
SearchResponse search(SearchRequest request)
次の例では、city フィールドが存在する行をクエリします。このクエリは、最大 10 行と一致総数を返します。
String tableName = "example_table";
String indexName = "example_index";
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("city");
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(existsQuery);
searchQuery.setLimit(10);
searchQuery.setTrackTotalCount(SearchQuery.TRACK_TOTAL_COUNT);
SearchRequest request = new SearchRequest(tableName, indexName, searchQuery);
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true);
request.setColumnsToGet(columnsToGet);
SearchResponse response = client.search(request);
System.out.println(response.getTotalCount());
System.out.println(response.getRows());
パラメーター
検索リクエスト
request は SearchRequest オブジェクトであり、以下のパラメーターを含みます。
|
名前 |
型 |
説明 |
|
tableName (必須) |
String |
データテーブルの名前。 |
|
indexName (必須) |
String |
多次元インデックスの名前。 |
|
searchQuery (必須) |
SearchQuery |
クエリ条件と一般的なクエリ設定。 |
|
columnsToGet (任意) |
SearchRequest.ColumnsToGet |
返すカラム。設定しない場合は、プライマリキーのカラムのみが返されます。 |
|
timeoutInMillisecond (任意) |
int |
ミリ秒単位のリクエストレベルのクエリタイムアウトです。デフォルト値は |
|
routingValues (任意) |
|
カスタムルーティングフィールドに対応するプライマリキーの値。カスタムルーティングを設定していない場合は、このパラメーターを設定しないでください。 |
クエリ設定
request.searchQuery は、以下のパラメーターを含む SearchQuery オブジェクトです。
|
名前 |
型 |
説明 |
|
query (必須) |
Query |
クエリ条件。exists クエリには、このパラメーターを |
|
offset (任意) |
Integer |
クエリの開始位置。 |
|
limit (任意) |
Integer |
返す行の最大数です。このパラメーターを |
|
collapse (任意) |
Collapse |
フィールドの折りたたみ設定。指定したフィールドで結果を重複排除します。設定の詳細については、「Collapse query results」をご参照ください。 |
|
sort (任意) |
Sort |
結果のソート順序。設定の詳細については、「Sort and paginate results」をご参照ください。 |
|
trackTotalCount (任意) |
int |
カウントする一致行の予想される最大数です。デフォルト値は |
|
filter (任意) |
SearchFilter |
|
|
aggregationList (任意) |
|
集計の設定。設定の詳細については、「Aggregation」をご参照ください。 |
|
groupByList (任意) |
|
グループ化の設定。設定の詳細については、「Aggregation」をご参照ください。 |
|
token (任意) |
byte[] |
ページネーション トークン。行の読み取りを続行するには、このパラメーターに前のレスポンスの |
クエリ条件
request.searchQuery.query は、以下のパラメーターを含む ExistsQuery オブジェクトです。
|
名前 |
型 |
説明 |
|
fieldName (必須) |
String |
クエリ対象となるインデックス付きフィールドの名前。 |
返されるカラム
request.columnsToGet は、次のパラメーターを含む SearchRequest.ColumnsToGet オブジェクトです。
|
名前 |
型 |
説明 |
|
columns (任意) |
|
返す属性列。このパラメーターは、 |
|
returnAll (任意) |
boolean |
データテーブルからすべての属性列を返すかどうかを指定します。デフォルト値は |
|
returnAllFromIndex (任意) |
boolean |
すべてのインデックス属性列を返すかどうかを指定します。 デフォルト値は |
戻り値
search は SearchResponse オブジェクトを返します。以下の表で、コアフィールドについて説明します。
|
名前 |
型 |
説明 |
|
totalCount |
long |
一致する行数です。 |
|
rows |
|
このクエリによって返される行。 |
|
searchHits |
|
クエリがヒットします。 |
|
nextToken |
byte[] |
次のページトークンです。 |
|
isAllSuccess |
boolean |
すべてのインデックスパーティションのクエリが成功したかどうかを示します。 |
シナリオ例
フィールドが存在しない行のクエリ
ExistsQuery を BoolQuery.mustNotQueries に追加して、city フィールドが存在しない行に一致させます。
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("city");
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustNotQueries(Collections.singletonList(existsQuery));
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(boolQuery);
Nested フィールドのクエリ
ExistsQuery を NestedQuery でラップします。親フィールドをクエリするには、fieldName を items に設定します。サブフィールドをクエリするには、items.keyword などのフルパスを指定します。次の例では、items.keyword サブフィールドが存在する行をクエリします。
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("items.keyword");
NestedQuery nestedQuery = new NestedQuery();
nestedQuery.setPath("items");
nestedQuery.setQuery(existsQuery);
nestedQuery.setScoreMode(ScoreMode.None);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(nestedQuery);