Exemplo n.º 1
0
        /// <summary>
        /// 查询表中geo_type_col这一列的值在一个给定多边形范围内的数据。
        /// </summary>
        /// <param name="client"></param>
        public static void GeoPolygonQuery(OTSClient client)
        {
            Console.WriteLine("\n Start GeoPolygon query...");

            SearchQuery     searchQuery     = new SearchQuery();
            GeoPolygonQuery geoPolygonQuery = new GeoPolygonQuery();  // 设置查询类型为GeoPolygonQuery

            geoPolygonQuery.FieldName = Geo_type_col;
            geoPolygonQuery.Points    = new List <string>()
            {
                "0,0", "10,0", "10,10"
            };                                                                      // 设置多边形的顶点
            searchQuery.Query = geoPolygonQuery;

            SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);

            ColumnsToGet columnsToGet = new ColumnsToGet();

            columnsToGet.Columns = new List <string>()
            {
                Geo_type_col
            };                                                           //设置返回Col_GeoPoint这一列
            searchRequest.ColumnsToGet = columnsToGet;

            SearchResponse response = client.Search(searchRequest);

            Console.WriteLine(response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// geo_type_col是GeoPoint类型,查询表中geo_type_col这一列的值在左上角为"10,0", 右下角为"0,10"的矩形范围内的数据
        /// </summary>
        /// <param name="client"></param>
        public static void GeoBoundingBoxQuery(OTSClient client)
        {
            Console.WriteLine("\n Start GeoBoundingBox query...");

            SearchQuery         searchQuery         = new SearchQuery();
            GeoBoundingBoxQuery geoBoundingBoxQuery = new GeoBoundingBoxQuery(); // 设置查询类型为GeoBoundingBoxQuery

            geoBoundingBoxQuery.FieldName   = Geo_type_col;                      // 设置比较哪个字段的值
            geoBoundingBoxQuery.TopLeft     = "10,0";                            // 设置矩形左上角
            geoBoundingBoxQuery.BottomRight = "0,10";                            // 设置矩形右下角
            searchQuery.Query = geoBoundingBoxQuery;

            SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);

            var columnsToGet = new ColumnsToGet();

            columnsToGet.Columns = new List <string> {
                Geo_type_col
            };                                                         //设置返回Col_GeoPoint这一列
            searchRequest.ColumnsToGet = columnsToGet;

            SearchResponse response = client.Search(searchRequest);

            Console.WriteLine(response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///  查询表中geo_type_col这一列的值距离中心点不超过一定距离的数据。
        /// </summary>
        /// <param name="client"></param>
        public static void GeoDistanceQuery(OTSClient client)
        {
            Console.WriteLine("\n Start GeoDistance query...");

            SearchQuery      searchQuery      = new SearchQuery();
            GeoDistanceQuery geoDistanceQuery = new GeoDistanceQuery();  // 设置查询类型为GeoDistanceQuery

            geoDistanceQuery.FieldName       = Geo_type_col;
            geoDistanceQuery.CenterPoint     = "10,11"; // 设置中心点
            geoDistanceQuery.DistanceInMeter = 10000;   // 设置到中心点的距离条件,不超过50米
            searchQuery.Query = geoDistanceQuery;

            SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);

            ColumnsToGet columnsToGet = new ColumnsToGet();

            columnsToGet.Columns = new List <string>()
            {
                Geo_type_col
            };                                                           //设置返回Col_GeoPoint这一列
            searchRequest.ColumnsToGet = columnsToGet;

            SearchResponse response = client.Search(searchRequest);

            Console.WriteLine(response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
        /// <summary>
        /// 有一个类型为NESTED的列,子文档包含nested_1和nested_2两列,现在查询col1_nested.nested_1为"tablestore"的数据
        /// </summary>
        /// <param name="otsClient"></param>
        public static void NestedQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start bool query...");

            var searchQuery = new SearchQuery();

            searchQuery.GetTotalCount = true;
            var nestedQuery = new NestedQuery();

            nestedQuery.Path = "col1_nested";                                                           //设置nested字段路径
            TermQuery termQuery = new TermQuery("col1_nested.nested_1", new ColumnValue("tablestore")); //构造NestedQuery的子查询

            nestedQuery.Query     = termQuery;
            nestedQuery.ScoreMode = ScoreMode.None;

            var request = new SearchRequest(TableName, IndexName, searchQuery);

            request.ColumnsToGet = new ColumnsToGet()
            {
                ReturnAll = true
            };

            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
        /// <summary>
        /// 精确的terms查询,查询Keyword_type_col这一列精确匹配"TableStore"或者"SearchIndex"的数据
        /// TermsQuery可以使用多个Term同时查询
        /// </summary>
        /// <param name="otsClient"></param>
        public static void TermsQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start terms query...");

            var searchQuery = new SearchQuery();

            searchQuery.GetTotalCount = true;
            var query = new TermsQuery();

            query.FieldName = Keyword_type_col;
            query.Terms     = new List <ColumnValue>()
            {
                new ColumnValue("TableStore"), new ColumnValue("SearchIndex")
            };

            var request = new SearchRequest(TableName, IndexName, searchQuery);

            request.ColumnsToGet = new ColumnsToGet()
            {
                ReturnAll = true
            };

            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
        /// <summary>
        ///联合查询(复杂查询条件下用的最多的一个查询)。Bool查询由一个或者多个子句组成,每个子句都有特定的类型。
        ///must: 文档必须完全匹配条件
        ///should: should下面会带一个以上的条件,至少满足一个条件,这个文档就符合should
        ///must_not: 文档必须不匹配条件
        ///MinimumShouldMatch: should查询的条件至少满足几个
        /// </summary>
        /// <param name="otsClient"></param>
        public static void BoolQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start bool query...");

            var searchQuery = new SearchQuery();

            searchQuery.GetTotalCount = true;
            var boolQuery    = new BoolQuery();
            var shouldQuerys = new List <IQuery>();

            shouldQuerys.Add(new TermQuery(Keyword_type_col, new ColumnValue("SearchIndex")));
            shouldQuerys.Add(new TermQuery(Keyword_type_col, new ColumnValue("TableStore")));
            boolQuery.ShouldQueries      = shouldQuerys;
            boolQuery.MinimumShouldMatch = 1;

            searchQuery.Query = boolQuery;

            var request = new SearchRequest(TableName, IndexName, searchQuery);

            request.ColumnsToGet = new ColumnsToGet()
            {
                ReturnAll = true
            };

            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
        /// <summary>
        /// 类似MatchQuery(MatchQuery 仅匹配某个词即可),但是 MatchPhraseQuery会匹配所有的短语。
        /// </summary>
        /// <param name="otsClient"></param>
        public static void MatchPhraseQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start MatchPhrase query...");

            var searchQuery = new SearchQuery();

            searchQuery.Query         = new MatchPhraseQuery(Text_type_col, "TableStore SearchIndex");
            searchQuery.GetTotalCount = true;
            var request = new SearchRequest(TableName, IndexName, searchQuery);

            request.ColumnsToGet = new ColumnsToGet()
            {
                Columns = new List <string>()
                {
                    Long_type_col, Text_type_col, Keyword_type_col
                }
            };

            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
        /// <summary>
        /// 等待数据同步到索引表完成
        /// </summary>
        /// <param name="otsClient"></param>
        /// <param name="expectTotalCount"></param>
        public static void WaiteAllDataSyncSuccess(OTSClient otsClient, int expectTotalCount)
        {
            Console.WriteLine("wait all rows sync success");
            int timeoutSeconds = 3 * 60;
            var beginTime      = DateTime.Now;

            while (true)
            {
                var searchQuery = new SearchQuery();
                searchQuery.GetTotalCount = true;
                searchQuery.Query         = new MatchAllQuery();
                var request = new SearchRequest(TableName, IndexName, searchQuery);

                var response = otsClient.Search(request);
                if (response.TotalCount == expectTotalCount)
                {
                    break;
                }
                else if ((DateTime.Now - beginTime).Seconds > timeoutSeconds)
                {
                    throw new Exception("searchIndex sync data timeout");
                }
                Thread.Sleep(1000);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 查询所有行,返回行数
        /// </summary>
        /// <param name="otsClient"></param>
        public static SearchResponse ReadMoreRowsWithToken(OTSClient otsClient)
        {
            Console.WriteLine("\n Start matchAll query...");

            var searchQuery = new SearchQuery();

            searchQuery.Query = new MatchAllQuery();

            var request = new SearchRequest(TableName, IndexName, searchQuery);

            var response = otsClient.Search(request);
            var rows     = response.Rows;

            while (response.NextToken != null)
            {
                request.SearchQuery.Token = response.NextToken;
                response = otsClient.Search(request);
                rows.AddRange(response.Rows);
            }

            return(response);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 查询所有行,返回行数
        /// </summary>
        /// <param name="otsClient"></param>
        public static void MatchAllQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start matchAll query...");

            var searchQuery = new SearchQuery();

            searchQuery.Query = new MatchAllQuery();
            var request = new SearchRequest(TableName, IndexName, searchQuery);

            var response = otsClient.Search(request);

            Console.WriteLine("IsAllSuccess:" + response.IsAllSuccess);
            Console.WriteLine("Total Count:" + response.TotalCount);
        }
        /// <summary>
        /// 范围查询。通过设置一个范围(from,to),查询该范围内的所有数据。
        /// </summary>
        /// <param name="otsClient"></param>
        public static void RangeQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start range query...");

            var searchQuery = new SearchQuery();

            searchQuery.GetTotalCount = true;
            var rangeQuery = new RangeQuery(Long_type_col, new ColumnValue(0), new ColumnValue(6));

            //包括下边界(大于等于0)
            rangeQuery.IncludeLower = true;
            searchQuery.Query       = rangeQuery;
            var request  = new SearchRequest(TableName, IndexName, searchQuery);
            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }
        /// <summary>
        /// 查询所有行,返回行数
        /// </summary>
        /// <param name="otsClient"></param>
        public static void MatchAllQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start matchAll query...");

            var searchQuery = new SearchQuery();

            searchQuery.Query         = new MatchAllQuery();
            searchQuery.GetTotalCount = true; // 需要设置GetTotalCount = true 才会返回满足条件的数据总行数

            /*
             * MatchAllQuery 结果中的Totalcount可以表示数据的总行数(数据量很大时为估算值)
             * 如果只是为了查询TotalHit,可以设置limit=0,即不返回任意一行数据。
             */
            searchQuery.Limit = 0;
            var request = new SearchRequest(TableName, IndexName, searchQuery);

            var response = otsClient.Search(request);

            Console.WriteLine("IsAllSuccess:" + response.IsAllSuccess);
            Console.WriteLine("Total Count:" + response.TotalCount);
        }
        /// <summary>
        ///通配符查询。支持 *( 任意0或多个)和 ?(任意1个字符)。
        /// </summary>
        /// <param name="otsClient"></param>
        public static void WildcardQuery(OTSClient otsClient)
        {
            Console.WriteLine("\n Start wildcard query...");

            var searchQuery = new SearchQuery();

            searchQuery.Query         = new WildcardQuery(Keyword_type_col, "*Search*");
            searchQuery.GetTotalCount = true;
            var request = new SearchRequest(TableName, IndexName, searchQuery);

            request.ColumnsToGet = new ColumnsToGet()
            {
                ReturnAll = true
            };

            var response = otsClient.Search(request);

            Console.WriteLine("Total Count:" + response.TotalCount);
            foreach (var row in response.Rows)
            {
                PrintRow(row);
            }
        }