public async Task SearchTestAsync()
        {
            BatchIndexingResponse addObjectResponse =
                await _index.SaveObjectsAsync(_employees, autoGenerateObjectId : true);

            addObjectResponse.Wait();

            Assert.IsInstanceOf <BatchIndexingResponse>(addObjectResponse);
            Assert.NotNull(addObjectResponse);

            IndexSettings settings = new IndexSettings
            {
                AttributesForFaceting = new List <string> {
                    "searchable(company)"
                }
            };
            var setSettingsResponse = await _index.SetSettingsAsync(settings);

            setSettingsResponse.Wait();

            Assert.IsInstanceOf <SetSettingsResponse>(setSettingsResponse);
            Assert.NotNull(setSettingsResponse);

            Task <SearchResponse <Employee> > searchAlgoliaTask = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery = "algolia"
            });

            Task <SearchResponse <Employee> > searchElonTask = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery    = "elon",
                ClickAnalytics = true
            });

            Task <SearchResponse <Employee> > searchElonTask1 = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery = "elon",
                Facets      = new List <string> {
                    "*"
                },
                FacetFilters = new List <List <string> > {
                    new List <string> {
                        "company:tesla"
                    }
                }
            });

            Task <SearchResponse <Employee> > searchElonTask2 = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery = "elon",
                Facets      = new List <string> {
                    "*"
                },
                Filters = "(company:tesla OR company:spacex)"
            });

            Task <SearchForFacetResponse> searchFacetTask = _index.SearchForFacetValueAsync(new SearchForFacetRequest
            {
                FacetName  = "company",
                FacetQuery = "a"
            });

            Task.WaitAll(searchAlgoliaTask, searchElonTask, searchElonTask1, searchElonTask2, searchFacetTask);

            Assert.That(searchAlgoliaTask.Result.Hits, Has.Exactly(2).Items);
            Assert.That(searchAlgoliaTask.Result.GetObjectPosition("nicolas-dessaigne"), Is.EqualTo(0));
            Assert.That(searchAlgoliaTask.Result.GetObjectPosition("julien-lemoine"), Is.EqualTo(1));
            Assert.That(searchAlgoliaTask.Result.GetObjectPosition(""), Is.EqualTo(-1));
            Assert.That(searchElonTask.Result.QueryID, Is.Not.Null);
            Assert.That(searchElonTask1.Result.Hits, Has.Exactly(1).Items);
            Assert.That(searchElonTask2.Result.Hits, Has.Exactly(2).Items);
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Algolia")));
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Amazon")));
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Apple")));
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Arista Networks")));

            Assert.IsNull(_index.FindObject <Employee>(x => false, new Query("")));
            var alwaysTrue = _index.FindObject <Employee>(x => true, new Query(""));

            Assert.That(alwaysTrue.Position, Is.EqualTo(0));
            Assert.That(alwaysTrue.Page, Is.EqualTo(0));
            Assert.IsNull(_index.FindObject <Employee>(x => x.Company.Equals("Apple"), new Query("algolia")));
            Assert.IsNull(_index.FindObject <Employee>(x => x.Company.Equals("Apple"), new Query("")
            {
                HitsPerPage = 5
            },
                                                       false));
            var foundObject =
                _index.FindObject <Employee>(x => x.Company.Equals("Apple"), new Query("")
            {
                HitsPerPage = 5
            });

            Assert.That(foundObject.Position, Is.EqualTo(0));
            Assert.That(foundObject.Page, Is.EqualTo(2));
        }
Beispiel #2
0
        public async Task SearchTestAsync()
        {
            BatchIndexingResponse addObjectResponse =
                await _index.SaveObjectsAsync(_employees, autoGenerateObjectId : true);

            addObjectResponse.Wait();

            Assert.IsInstanceOf <BatchIndexingResponse>(addObjectResponse);
            Assert.NotNull(addObjectResponse);

            IndexSettings settings = new IndexSettings
            {
                AttributesForFaceting = new List <string> {
                    "searchable(company)"
                }
            };
            var setSettingsResponse = await _index.SetSettingsAsync(settings);

            setSettingsResponse.Wait();

            Assert.IsInstanceOf <SetSettingsResponse>(setSettingsResponse);
            Assert.NotNull(setSettingsResponse);

            Task <SearchResponse <Employee> > searchAlgoliaTask = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery = "algolia"
            });

            Task <SearchResponse <Employee> > searchElonTask = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery    = "elon",
                ClickAnalytics = true
            });

            Task <SearchResponse <Employee> > searchElonTask1 = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery = "elon",
                Facets      = new List <string> {
                    "*"
                },
                FacetFilters = new List <List <string> > {
                    new List <string> {
                        "company:tesla"
                    }
                }
            });

            Task <SearchResponse <Employee> > searchElonTask2 = _index.SearchAsync <Employee>(new Query
            {
                SearchQuery = "elon",
                Facets      = new List <string> {
                    "*"
                },
                Filters = "(company:tesla OR company:spacex)"
            });

            Task <SearchForFacetResponse> searchFacetTask = _index.SearchForFacetValueAsync(new SearchForFacetRequest
            {
                FacetName  = "company",
                FacetQuery = "a"
            });

            Task.WaitAll(searchAlgoliaTask, searchElonTask, searchElonTask1, searchElonTask2, searchFacetTask);

            Assert.IsTrue(searchAlgoliaTask.Result.Hits.Count == 2);
            Assert.IsTrue(searchElonTask.Result.QueryID != null);
            Assert.IsTrue(searchElonTask1.Result.Hits.Count == 1);
            Assert.IsTrue(searchElonTask2.Result.Hits.Count == 2);
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Algolia")));
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Amazon")));
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Apple")));
            Assert.IsTrue(searchFacetTask.Result.FacetHits.Any(x => x.Value.Equals("Arista Networks")));
        }
        public async Task SynonymsOperationsTest()
        {
            BatchIndexingResponse addObjectResponse =
                await _index.SaveObjectsAsync(_objectsToSave, autoGenerateObjectId : true);

            addObjectResponse.Wait();

            Synonym gba = new Synonym
            {
                ObjectID = "gba",
                Type     = SynonymType.Synonym,
                Synonyms = new List <string> {
                    "gba", "gameboy advance", "game boy advance"
                }
            };

            var regularSynonymResponse = await _index.SaveSynonymAsync(gba);

            Synonym wiiToWiiu = new Synonym
            {
                ObjectID = "wii_to_wii_u",
                Type     = SynonymType.OneWaySynonym,
                Input    = "wii",
                Synonyms = new List <string> {
                    "wii u"
                }
            };

            Synonym playstationPlaceholder = new Synonym
            {
                ObjectID     = "playstation_version_placeholder",
                Type         = SynonymType.Placeholder,
                Placeholder  = "<PLAYSTATIONVERSION>",
                Replacements = new List <string>
                {
                    "1",
                    "One",
                    "2",
                    "3",
                    "4",
                    "4 Pro"
                }
            };

            Synonym ps4 = new Synonym
            {
                ObjectID    = "ps4",
                Type        = SynonymType.AltCorrection1,
                Word        = "ps4",
                Corrections = new List <string> {
                    "playstation4"
                }
            };

            Synonym psone = new Synonym
            {
                ObjectID    = "psone",
                Type        = SynonymType.AltCorrection2,
                Word        = "psone",
                Corrections = new List <string> {
                    "playstationone"
                }
            };

            List <Synonym> synonyms = new List <Synonym> {
                wiiToWiiu, playstationPlaceholder, ps4, psone
            };

            var saveSynonymsResponse = await _index.SaveSynonymsAsync(synonyms);

            regularSynonymResponse.Wait();
            saveSynonymsResponse.Wait();

            // Retrieve the 5 added synonyms with getSynonym and check they are correctly retrieved
            var gbaTask = _index.GetSynonymAsync("gba");
            var wiiTask = _index.GetSynonymAsync("wii_to_wii_u");
            var playstationPlaceholderTask = _index.GetSynonymAsync("playstation_version_placeholder");
            var ps4Task   = _index.GetSynonymAsync("ps4");
            var psoneTask = _index.GetSynonymAsync("psone");

            Synonym[] tasks = await Task.WhenAll(gbaTask, wiiTask, playstationPlaceholderTask, ps4Task, psoneTask);

            Assert.True(TestHelper.AreObjectsEqual(gba, tasks[0]));
            Assert.True(TestHelper.AreObjectsEqual(wiiToWiiu, tasks[1]));
            Assert.True(TestHelper.AreObjectsEqual(playstationPlaceholder, tasks[2]));
            Assert.True(TestHelper.AreObjectsEqual(ps4, tasks[3]));
            Assert.True(TestHelper.AreObjectsEqual(psone, tasks[4]));

            // Perform a synonym search using searchSynonyms with an empty query, page 0 and hitsPerPage set to 10 and check that the returned synonyms are the same as the 5 originally saved
            SearchResponse <Synonym> searchResponse =
                await _index.SearchSynonymsAsync(new SynonymQuery { HitsPerPage = 10, Page = 0 });

            Assert.That(searchResponse.Hits, Has.Exactly(5).Items);

            // Instantiate a new SynonymIterator using newSynonymIterator and iterate over all the synonyms and check that those collected synonyms are the same as the 5 originally saved
            List <Synonym> synonymsFromIterator = new List <Synonym>();

            foreach (var synonym in new SynonymsIterator(_index))
            {
                synonymsFromIterator.Add(synonym);
            }

            Assert.True(TestHelper.AreObjectsEqual(gba, synonymsFromIterator.Find(s => s.ObjectID.Equals("gba"))));
            Assert.True(TestHelper.AreObjectsEqual(wiiToWiiu,
                                                   synonymsFromIterator.Find(s => s.ObjectID.Equals("wii_to_wii_u"))));
            Assert.True(TestHelper.AreObjectsEqual(playstationPlaceholder,
                                                   synonymsFromIterator.Find(s => s.ObjectID.Equals("playstation_version_placeholder"))));
            Assert.True(TestHelper.AreObjectsEqual(ps4, synonymsFromIterator.Find(s => s.ObjectID.Equals("ps4"))));
            Assert.True(TestHelper.AreObjectsEqual(psone, synonymsFromIterator.Find(s => s.ObjectID.Equals("psone"))));

            // Delete the synonym with objectID=”gba” using deleteSynonym and wait for the task to terminate using waitTask with the returned taskID
            var deleteGbaResponse = await _index.DeleteSynonymAsync("gba");

            deleteGbaResponse.Wait();

            // Try to get the synonym with getSynonym with objectID “gba” and check that the synonym does not exist anymore (404)
            AlgoliaApiException ex = Assert.ThrowsAsync <AlgoliaApiException>(() => _index.GetSynonymAsync("gba"));

            Assert.That(ex.HttpErrorCode, Is.EqualTo(404));

            // Clear all the synonyms using clearSynonyms and wait for the task to terminate using waitTask with the returned taskID
            var clearSynonymResponse = await _index.ClearSynonymsAsync();

            clearSynonymResponse.Wait();

            // Perform a synonym search using searchSynonyms with an empty query, page 0 and hitsPerPage set to 10 and check that the number of returned synonyms is equal to 0
            SearchResponse <Synonym> searchAfterClearResponse =
                await _index.SearchSynonymsAsync(new SynonymQuery { HitsPerPage = 10, Page = 0 });

            Assert.That(searchAfterClearResponse.Hits, Is.Empty);
        }
Beispiel #4
0
        /// <inheritdoc />
        public async Task <MultiResponse> CopyIndexAsync <T>(ISearchIndex sourceIndex, ISearchIndex destinationIndex,
                                                             RequestOptions requestOptions = null, CancellationToken ct = default) where T : class
        {
            if (sourceIndex.Config.AppId.Equals(destinationIndex.Config.AppId))
            {
                throw new AlgoliaException("Source and Destination indices should not be on the same application.");
            }

            // TODO: improve this section
            try
            {
                IndexSettings destinationSettings =
                    await destinationIndex.GetSettingsAsync(ct : ct).ConfigureAwait(false);

                if (destinationSettings != null)
                {
                    throw new AlgoliaException(
                              "Destination index already exists. Please delete it before copying index across applications.");
                }
            }
            catch (AlgoliaApiException ex)
            {
                // We want to catch an non existing index exception (404) and continue
                // Otherwise, we want to throw if it's another Http exception
                if (ex.HttpErrorCode != 404)
                {
                    throw;
                }
            }

            MultiResponse ret = new MultiResponse {
                Responses = new List <IAlgoliaWaitableResponse>()
            };

            // Save settings
            IndexSettings sourceSettings = await sourceIndex.GetSettingsAsync(ct : ct).ConfigureAwait(false);

            SetSettingsResponse destinationSettingsResp = await destinationIndex
                                                          .SetSettingsAsync(sourceSettings, requestOptions, ct)
                                                          .ConfigureAwait(false);

            ret.Responses.Add(destinationSettingsResp);

            // Save synonyms
            SynonymsIterator    sourceSynonyms             = new SynonymsIterator(sourceIndex);
            SaveSynonymResponse destinationSynonymResponse = await destinationIndex
                                                             .SaveSynonymsAsync(sourceSynonyms, requestOptions : requestOptions, ct : ct)
                                                             .ConfigureAwait(false);

            ret.Responses.Add(destinationSynonymResponse);

            // Save rules
            RulesIterator sourceRules             = new RulesIterator(sourceIndex);
            BatchResponse destinationRuleResponse = await destinationIndex
                                                    .SaveRulesAsync(sourceRules, requestOptions : requestOptions, ct : ct)
                                                    .ConfigureAwait(false);

            ret.Responses.Add(destinationRuleResponse);

            // Save objects (batched)
            IndexIterator <T>     indexIterator = sourceIndex.Browse <T>(new BrowseIndexQuery());
            BatchIndexingResponse saveObject    = await destinationIndex
                                                  .SaveObjectsAsync(indexIterator, requestOptions, ct)
                                                  .ConfigureAwait(false);

            ret.Responses.Add(saveObject);

            return(ret);
        }