Esempio n. 1
0
        public static async Task <IApiResponse <SearchQueryResult> > Search(string search, string[] facets)
        {
            var conn        = ApiConnection.Create(Keys.ListingsServiceUrl, Keys.ListingsServiceKey);
            var queryClient = new IndexQueryClient(conn);
            var query       = new SearchQuery(search)
                              .Count(true)
                              .Select("Id,Color,Options,Type,Package,Image")
                              .OrderBy("Color");

            if (facets != null)
            {
                StringBuilder facet = new StringBuilder();
                foreach (string f in facets)
                {
                    facet.Append(f + ",");
                }
                string facetResult = facet.ToString();
                //trim trailing comma
                facetResult = facetResult.Substring(0, facetResult.Count() - 1);
                query.Facet(facetResult);
            }
            var searchResults = await queryClient.SearchAsync(Keys.ListingsServiceIndexName, query);

            return(searchResults);
        }
        private async Task <QueryResultsModel> ExecuteSearch(string index, string query, string searchFields, string filter, string orderBy, int skip, int take)
        {
            var connection        = ApiConnection.Create(_searchInstance, _key);
            var searchFieldsArray = searchFields.Split(',');
            var orderByField      = string.IsNullOrEmpty(orderBy) && searchFieldsArray.Length > 0 ? searchFieldsArray[0] : orderBy;

            var client           = new IndexQueryClient(connection);
            var searchQueryModel = new SearchQuery(query);

            searchQueryModel.SearchFields = searchFields;
            if (!string.IsNullOrWhiteSpace(filter))
            {
                searchQueryModel = searchQueryModel.Filter(filter);
            }

            //searchQueryModel.Facets = new[] { "TypeOfEstablishment", "OverallPhase", "ReligiousCharacter", "OfstedRating" };
            searchQueryModel.OrderBy = orderByField;
            searchQueryModel         = searchQueryModel.Mode(SearchMode.All).Count(true).Skip(skip).Top(take);

            var response = await client.SearchAsync(index, searchQueryModel);

            if (!response.IsSuccess)
            {
                throw new ApplicationException($"Azure Search trust search error {response.Error.Code}: {response.Error.Message}");
            }

            return(new QueryResultsModel(response.Body.Count, response.Body.Facets, response.Body.Records.Select(x => x.Properties), take, skip));
        }
Esempio n. 3
0
        /// <summary>
        /// 検索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void button4_Click(object sender, EventArgs e)
        {
            //インデクサ実行終了までにはAPIのレスポンス帰ってきてから少し時間がかかる模様 とりあえずwaitする
            if (!flg)
            {
                MessageBox.Show("インデックスを作成してね。作成したら少し時間をおいて(5秒くらい)アクセスしてね");
                return;
            }
            var connection = ApiConnection.Create("datest01", "A53D975119F80AA1D75895645A5234BE");

            // 検索を行う場合は IndexQueryClient を使う
            var c = new IndexQueryClient(connection);

            textBox2.Clear();

            for (int i = 0; i < fileNames.Length; i++)
            {
                // SearchQueryの引数が検索したい文字列
                var result = await c.SearchAsync("index" + i, new SearchQuery(textBox1.Text).Count(true));

                if (result.StatusCode == HttpStatusCode.NotFound)
                {
                    continue;
                }



                foreach (var item in result.Body.Records)
                {
                    textBox2.AppendText(string.Format("filename={0}\n", item.Properties["metadata_storage_name"]));
                }
            }
        }
Esempio n. 4
0
        public async Task <HttpResponseMessage> Get(string indexName, [FromUri] SearchQuery searchQuery)
        {
            var result = await _searchClient.SearchAsync(indexName, searchQuery);

            if (!result.IsSuccess)
            {
                return(Request.CreateResponse(result.StatusCode, result));
            }
            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
        public string GetInstalledVersion(string schema)
        {
            var queryClient = new IndexQueryClient(_db);
            var results     = queryClient.SearchAsync("automaticmigration", new SearchQuery(schema)
                                                      .OrderBy("TimeOfUpdate")
                                                      .SearchField("SchemaName")
                                                      .Top(1)
                                                      .Count(true)).Result;

            if (results == null || results.IsSuccess == false || results.Body == null || results.Body.Count < 1)
            {
                return(null);
            }

            return(results.Body.Records.SingleOrDefault().Properties["Version"].ToString());
        }
        public async Task <QueryResultsModel> ExecuteGeoSpatialSearch(string index, string lat, string lon,
                                                                      decimal distance, string filter, string orderBy, int skip, int take)
        {
            const string search    = "*";
            var          latitude  = double.Parse(lat);
            var          longitude = double.Parse(lon);

            var filterBuilder = new StringBuilder();
            var orderByField  = string.IsNullOrEmpty(orderBy)
                ? string.Format(GeoDistanceLocationOrderFormat, latitude, longitude)
                : orderBy;

            filterBuilder.AppendFormat(GeoDistanceLocationSearchFormat, latitude, longitude, distance);
            if (!string.IsNullOrEmpty(filter))
            {
                filterBuilder.AppendFormat(" and " + filter);
            }

            var connection = ApiConnection.Create(_searchInstance, _key);

            var client = new IndexQueryClient(connection);

            var response = await client.SearchAsync(index, new SearchQuery(search)
                                                    .OrderBy(orderByField)
                                                    .Facet("TypeOfEstablishment")
                                                    .Facet("OverallPhase")
                                                    .Facet("ReligiousCharacter")
                                                    .Facet("OfstedRating")
                                                    .Count(true)
                                                    .Filter(filterBuilder.ToString())
                                                    .Skip(skip)
                                                    .Top(take));

            if (!response.IsSuccess)
            {
                Console.WriteLine("{0}: {1}", response.Error.Code, response.Error.Message);
            }

            var results = new QueryResultsModel(response.Body.Count, response.Body.Facets,
                                                CalcDistance(response.Body.Records.Select(x => x.Properties), lat, lon), take, skip)
            {
                QueryLat  = latitude.ToString(),
                QueryLong = longitude.ToString()
            };

            return(results);
        }
        private async Task <QueryResultsModel> ExecuteSearch(string index, string query, string searchFields,
                                                             string filter, string orderBy, int skip, int take, IEnumerable <string> facets)
        {
            var set = _aliases.FirstOrDefault(x => x.Any(a => AliasFound(query, a)));

            if (set != null)
            {
                var word = set.FirstOrDefault(x => AliasFound(query, x));
                query = query.Replace(word, string.Concat("(", string.Join("|", set), ") "), true);
            }

            var connection        = ApiConnection.Create(_searchInstance, _key);
            var searchFieldsArray = searchFields.Split(',');
            var orderByField      = string.IsNullOrEmpty(orderBy) && searchFieldsArray.Length > 0
                ? searchFieldsArray[0].Replace("EstablishmentName", "EstablishmentNameUpperCase")
                : orderBy.Replace("EstablishmentName", "EstablishmentNameUpperCase");

            var client           = new IndexQueryClient(connection);
            var searchQueryModel = new SearchQuery(query);

            searchQueryModel.SearchFields = searchFields;
            if (!string.IsNullOrWhiteSpace(filter))
            {
                searchQueryModel = searchQueryModel.Filter(filter);
            }
            searchQueryModel.Facets  = facets;
            searchQueryModel.OrderBy = orderByField;
            searchQueryModel         = searchQueryModel.Mode(SearchMode.All).Count(true).Skip(skip).Top(take);

            var response = await client.SearchAsync(index, searchQueryModel);

            if (!response.IsSuccess)
            {
                throw new ApplicationException(
                          $"Edubase school search error {response.Error.Code}: {response.Error.Message}");
            }

            return(new QueryResultsModel(response.Body.Count, response.Body.Facets,
                                         response.Body.Records.Select(x => x.Properties), take, skip));
        }
        public void UntrackSchemas(IEnumerable <string> schemas)
        {
            var queryClient = new IndexQueryClient(_db);

            foreach (var schema in schemas)
            {
                var migrations = queryClient.SearchAsync("automaticmigration", new SearchQuery(schema)
                                                         .OrderBy("TimeOfUpdate")
                                                         .SearchField("SchemaName")).Result;

                if (!migrations.IsSuccess)
                {
                    throw new ApplicationException("Error untracking schema: could not find migrations in schema " + schema + ", error: " + migrations.Error.Message);
                }

                if (migrations.Body != null)
                {
                    var indexClient = new IndexManagementClient(_db);
                    var result      = indexClient.PopulateAsync("automaticmigration",
                                                                migrations.Body.Records.Select(record =>
                                                                                               new IndexOperation(IndexOperationType.Delete, "Id", record.Properties["Id"].ToString())).ToArray()).Result;
                }
            }
        }
        public async Task<List<SpotSearchResult>> GetSpotsByName(string name)
        {

            var client = new IndexQueryClient(connection);
            var results = await client.SearchAsync("spot", new SearchQuery(name)
                //.OrderBy("posttext")
                .SearchField("name").Top(500));

            if (!results.IsSuccess) throw new Exception(
                String.Format("Query failed, resaon: {0}", results.StatusCode.ToString())
                );

            List<SpotSearchResult> spotresult = new List<SpotSearchResult>();

            foreach (SearchQueryRecord record in results.Body.Records)
            {
                SpotSearchResult result = new SpotSearchResult();
                result.Uid = record.Properties["id"].ToString();
                result.Name = record.Properties["name"].ToString();
                result.Description = record.Properties["description"].ToString();

                if (record.Properties["location"] != null)
                {
                    dynamic test = JsonConvert.DeserializeObject(record.Properties["location"].ToString());

                    result.Location = new Location();
                    result.Location.Latitude = Convert.ToDouble(test.coordinates.Last.ToString());
                    result.Location.Longitude = Convert.ToDouble(test.coordinates.First.ToString());

                    spotresult.Add(result);
                }
            }


            return spotresult;
        }
        public async Task<List<SpotSearchResult>> GetSpotsNearby(double longitude, double latitude)
        {

            var client = new IndexQueryClient(connection);
            var results = await client.SearchAsync("spot", new SearchQuery()
                //.OrderBy("posttext")
                .SearchField("name")
                //geo.distance(location, geography'POINT(<long> <lat>)'
                .Filter(String.Format("geo.distance(location, geography'POINT({0} {1})') le 50",
                    longitude.ToString(CultureInfo.InvariantCulture.NumberFormat),
                    latitude.ToString(CultureInfo.InvariantCulture.NumberFormat)
                    ))
                );

            if (!results.IsSuccess) throw new Exception(
                String.Format("Query failed, resaon: {0}", results.StatusCode.ToString())
                );

            List<SpotSearchResult> spotresult = new List<SpotSearchResult>();

            foreach (SearchQueryRecord record in results.Body.Records)
            {
                SpotSearchResult result = new SpotSearchResult();
                result.Uid = record.Properties["id"].ToString();
                result.Name = record.Properties["name"].ToString();
                result.Description = record.Properties["description"].ToString();

                dynamic test = JsonConvert.DeserializeObject(record.Properties["location"].ToString());
                result.Location.Latitude = Convert.ToDouble(test.coordinates.Last.ToString(), CultureInfo.InvariantCulture.NumberFormat);
                result.Location.Longitude = Convert.ToDouble(test.coordinates.First.ToString(), CultureInfo.InvariantCulture.NumberFormat);
                spotresult.Add(result);
            }

            return spotresult;
        }