Boolean sorgu, birden çok alt sorguyu birleştirerek veri tablosundan satır getirir. Tablestore yalnızca birleşik koşullarla eşleşen satırları döndürür. Alt sorgu herhangi bir türde olabilir; başka bir Boolean sorgu da içerebilir.
Önkoşullar
Bir OTSClient örneği başlatılmış olmalıdır. Daha fazla bilgi için bkz. OTSClient örneğini başlatma.
Veri tablosu oluşturulmuş ve tabloya veri yazılmış olmalıdır. Daha fazla bilgi için bkz. Veri tablosu oluşturma ve Veri yazma.
Veri tablosu için arama dizini oluşturulmuş olmalıdır. Daha fazla bilgi için bkz. Arama dizini oluşturma.
Parametreler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VEYA işleci. Tablestore en az asgari sayıda alt sorgu koşuluyla eşleşen satırları döndürür. Bir satırın alaka puanı, eşleştiği her ek Bir satırın eşleşmesi gereken asgari koşul sayısını ayarlamak için |
|
|
|
Örnekler
Aşağıdaki örneklerde Boolean sorgusunun her bir yan tümcesinin nasıl kullanılacağı gösterilmektedir. Tüm örneklerde sorgu türü olarak QueryTypeConst::BOOL_QUERY belirtilerek $otsClient->search($request) çağrısı yapılır.
AND
Tüm koşulların sağlanmasını zorunlu kılmak için must_queries parametresini kullanın. Aşağıdaki örnekte keyword alanının değeri "keyword" olan VE long alanının değeri [100, 101) aralığında bulunan satırlar döndürülür.
$request = array(
'table_name' => 'php_sdk_test',
'index_name' => 'php_sdk_test_search_index',
'search_query' => array(
'offset' => 0,
'limit' => 2,
'get_total_count' => true,
'query' => array(
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => array(
'must_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
)
),
'sort' => array(
array(
'field_sort' => array(
'field_name' => 'keyword',
'order' => SortOrderConst::SORT_ORDER_ASC
)
),
)
),
'columns_to_get' => array(
'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
'return_names' => array('keyword', 'long')
)
);
$response = $otsClient->search($request);
OR
Herhangi bir koşulla eşleşmeyi sağlamak için should_queries parametresini minimum_should_match ile birlikte kullanın. Aşağıdaki örnekte keyword alanının değeri "keyword" olan VEYA long alanının değeri [100, 101) aralığında bulunan satırlar döndürülür.
$request = array(
'table_name' => 'php_sdk_test',
'index_name' => 'php_sdk_test_search_index',
'search_query' => array(
'offset' => 0,
'limit' => 2,
'get_total_count' => true,
'query' => array(
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => array(
'should_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
'minimum_should_match' => 1
)
),
'sort' => array(
array(
'field_sort' => array(
'field_name' => 'keyword',
'order' => SortOrderConst::SORT_ORDER_ASC
)
),
)
),
'columns_to_get' => array(
'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
'return_names' => array('keyword', 'long')
)
);
$response = $otsClient->search($request);
NOT
Koşullardan herhangi biriyle eşleşen satırları hariç tutmak için must_not_queries parametresini kullanın. Aşağıdaki örnekte keyword alanının değeri "keyword" olan VEYA long alanının değeri [100, 101) aralığında bulunan satırlar hariç tutulur; yani DEĞİL(koşul A VEYA koşul B) uygulanır.
$request = array(
'table_name' => 'php_sdk_test',
'index_name' => 'php_sdk_test_search_index',
'search_query' => array(
'offset' => 0,
'limit' => 2,
'get_total_count' => true,
'query' => array(
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => array(
'must_not_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
)
),
'sort' => array(
array(
'field_sort' => array(
'field_name' => 'keyword',
'order' => SortOrderConst::SORT_ORDER_ASC
)
),
)
),
'columns_to_get' => array(
'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
'return_names' => array('keyword', 'long')
)
);
$response = $otsClient->search($request);
filter_queries
Alaka puanı hesaplamadan VE mantığı uygulamak istediğinizde filter_queries parametresini kullanın. Aşağıdaki örnekte her iki koşulla da eşleşen satırlar döndürülür ancak satırlar kaç koşulla eşleştiklerine göre puanlanmaz.
$request = array(
'table_name' => 'php_sdk_test',
'index_name' => 'php_sdk_test_search_index',
'search_query' => array(
'offset' => 0,
'limit' => 2,
'get_total_count' => true,
'query' => array(
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => array(
'filter_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
)
),
'sort' => array(
array(
'field_sort' => array(
'field_name' => 'keyword',
'order' => SortOrderConst::SORT_ORDER_ASC
)
),
)
),
'columns_to_get' => array(
'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
'return_names' => array('keyword', 'long')
)
);
$response = $otsClient->search($request);
Birleştirilmiş sorgular
Karmaşık mantık yapıları oluşturmak için Boolean sorgularını iç içe yerleştirin. Aşağıdaki örnekte şu koşulu sağlayan satırlar eşleştirilir: (col2 < 4 OR col3 < 5) OR (col2 = 4 AND (col3 = 5 OR col3 = 6)).
$request = array(
'table_name' => 'php_sdk_test',
'index_name' => 'php_sdk_test_index',
'search_query' => [
'offset' => 0,
'limit' => 10,
'get_total_count' => false,
'query' => [
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => [
// Final combination: (col2 < 4 or col3 < 5) or (col2 = 4 and (col3 = 5 or col3 = 6))
'should_queries' => [
[
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => [
// Combination 1: col2 < 4 or col3 < 5
'should_queries' => [
[
'query_type' => QueryTypeConst::RANGE_QUERY,
// Condition 1: col2 < 4
'query' => [
'field_name' => 'col2',
'range_to' => 4
]
],
[
'query_type' => QueryTypeConst::RANGE_QUERY,
// Condition 2: col3 < 5
'query' => [
'field_name' => 'col3',
'range_to' => 5
]
]
]
]
],
[
'query_type' => QueryTypeConst::BOOL_QUERY,
// Combination 2: (col2 = 4 and (col3 = 5 or col3 = 6))
'query' => [
'must_queries' => [
[
'query_type' => QueryTypeConst::TERM_QUERY,
// Condition 3: col2 = 4
'query' => [
'field_name' => 'col2',
'term' => 4
]
],
[
'query_type' => QueryTypeConst::BOOL_QUERY,
// Combination 3: (col3 = 5 or col3 = 6)
'query' => [
'should_queries' => [
[
'query_type' => QueryTypeConst::TERM_QUERY,
// Condition 4: col3 = 5
'query' => [
'field_name' => 'col3',
'term' => 5
]
],
[
'query_type' => QueryTypeConst::TERM_QUERY,
// Condition 5: col3 = 6
'query' => [
'field_name' => 'col3',
'term' => 6
]
]
],
'minimum_should_match' => 1
]
]
]
]
]
],
'minimum_should_match' => 1
]
]
],
// You can configure the columns_to_get parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
'columns_to_get' => [
//'return_type' => ColumnReturnTypeConst::RETURN_ALL // Specify that all columns are returned.
// Specify that the specified columns are returned.
'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
'return_names' => array('col1', 'col2')
]
);
$response = $otsClient->search($request);
SSS
İlgili Başlıklar
-
Arama dizini kullanarak veri sorgularken aşağıdaki sorgu yöntemlerini kullanabilirsiniz: term sorgusu, terms sorgusu, match all sorgusu, match sorgusu, match phrase sorgusu, prefix sorgusu, range sorgusu, wildcard sorgusu, Boolean sorgu, geo sorgusu, nested sorgusu ve exists sorgusu. İş gereksinimlerinize göre farklı sorgu yöntemlerini kullanarak verileri birden çok boyutta sorgulayabilirsiniz.
Sorgu koşullarını karşılayan satırları sıralamak veya sayfalamak istiyorsanız sıralama ve sayfalama özelliğini kullanabilirsiniz. Daha fazla bilgi için bkz. Sıralama ve sayfalama.
Sonuç kümesini belirli bir sütuna göre daraltmak istiyorsanız daraltma (distinct) özelliğini kullanabilirsiniz. Bu şekilde belirtilen türe ait veriler sorgu sonuçlarında yalnızca bir kez görünür. Daha fazla bilgi için bkz. Daraltma (distinct).
Uç değerleri, toplamı ve toplam satır sayısını elde etmek gibi veri tablosundaki verileri analiz etmek istiyorsanız toplama işlemleri yapabilir veya SQL deyimleri çalıştırabilirsiniz. Daha fazla bilgi için bkz. Toplama ve SQL sorgusu.
Satırları sıralama ihtiyacı olmadan sorgu koşullarını karşılayan tüm satırları hızla elde etmek istiyorsanız paralel tarama özelliğini kullanmak üzere ParallelScan ve ComputeSplits işlemlerini çağırabilirsiniz. Daha fazla bilgi için bkz. Paralel tarama.