Allow you to search, faceted search, add, update, remove objects from your Amazon Cloud Search Index in C#.
Feel free to fork and improuve Amazing Cloud Search API.
I find work with uWorkin an awsome Melbourne based company using incredible technology. We chose to use Amazon Cloud Seach for it's powerfull search engine mechanism and specially for it's incredible faceleted search.
All my thanks to uWorkin who gave me the opportunity to work on and open source the first Amazon Cloud Search API for the C# community. http://www.uworkin.com
First you need a Amazon cloud search instance and it's URL (We will call it a key) It should look like : yourDomainName-xxxxxxxxxxxxx.us-east-1.cloudsearch.amazonaws.com
This exemple can be used with the IMDB default index that you can select when creating your Cloud Search.
It indexes movies which implement SearchDocument (just an ID):
public class Movie : SearchDocument
{
public List<string> actor { get; set; }
public string director { get; set; }
public DateTime mydate { get; set; }
public string title { get; set; }
public int year { get; set; }
}
The reason why my object field starts in lowercase is because YOU NEED to match the field name in your index. This needs to be improved (feel free)
var cloudSearch = new CloudSearch<Movie>("YOU_AMAZON_CLOUD_SEARCH_KEY", "2011-02-01");
var movie = new Movie { id = "fjuhewdijsdjoi", title = "simple title", year = 2012, mydate = DateTime.Now, actor = new List<string> { "good actor1", "good actor2" }, director = "martin magakian" };
cloudSearch.Add(movie);
var movie = new Movie { id = "fjuhewdijsdjoi" }
cloudSearch.Delete(movie);
movie.title = "In the skin of Amazon cloud search";
cloudSearch.Update(movie);
var searchQuery = new SearchQuery<Movie> {Keyword = "star wars"};
var found = cloudSearch.Search(searchQuery);
var bQuery = new BooleanQuery();
var gCondition = new StringBooleanCondition("genre", "Sci-Fi");
bQuery.Conditions.Add(gCondition);
var searchQuery = new SearchQuery<Movie> {Keyword = "star wars", Size = 25, BooleanQuery = bQuery};
var found = cloudSearch.Search(searchQuery);
var bQuery = new BooleanQuery();
var gCondition = new StringBooleanCondition("genre", "Sci-Fi");
var yCondition = new IntBooleanCondition("year");
yCondition.SetInterval(2000,2004);
bQuery.Conditions.Add(gCondition);
bQuery.Conditions.Add(yCondition);
var searchQuery = new SearchQuery<Movie> {Keyword = "star wars", Facets = liFacet, Size = 20, BooleanQuery = bQuery};
var found = cloudSearch.Search(searchQuery);
var genreFacet = new Facet { Name = "genre" };
var liFacet = new List<Facet> { genreFacet };
var searchQuery = new SearchQuery<Movie> {Keyword = "star wars"};
var found = cloudSearch.Search(searchQuery);
var genreFacetContraint = new StringFacetConstraints();
genreFacetContraint.AddContraint("Sci-Fi");
genreFacetContraint.AddContraint("Fantasy");
var genreFacet = new Facet { Name = "genre", FacetContraint = genreFacetContraint };
var liFacet = new List<Facet> { genreFacet };
var searchQuery = new SearchQuery<Movie> {Keyword = "star wars", Facets = liFacet};
var found = cloudSearch.Search(searchQuery);
Search for movies + number of results in the 'Sci-Fi' and 'Fantasy' categories + the number of result in the year 1950 and between 1980 and 2012 (faceted search)
var genreFacetContraint = new StringFacetConstraints();
genreFacetContraint.AddContraint("Sci-Fi");
genreFacetContraint.AddContraint("Fantasy");
var genreFacet = new Facet { Name = "genre", FacetContraint = genreFacetContraint };
var yearFacetContraint = new IntFacetContraints();
yearFacetContraint.AddFrom(1950);
yearFacetContraint.AddInterval(1980, 2012);
var yearFacet = new Facet { Name = "year", FacetContraint = yearFacetContraint };
var liFacet = new List<Facet> { genreFacet, yearFacet };
var searchQuery = new SearchQuery<Movie> {Keyword = "star wars", Facets = liFacet};
var found = cloudSearch.Search(searchQuery);
All together now: Facet for Sci-Fi,Fantasy, in 1950, between 1980 to 2012 + search only for movies in Sci-Fi from 2000 to 2004
var cloudSearch = new CloudSearch<Movie>("YOUR_CLOUD_SEARCH_API", "2011-02-01");
//build facet
var genreFacetContraint = new StringFacetConstraints();
genreFacetContraint.AddContraint("Sci-Fi");
genreFacetContraint.AddContraint("Fantasy");
var genreFacet = new Facet { Name = "genre", FacetContraint = genreFacetContraint };
var yearFacetContraint = new IntFacetContraints();
yearFacetContraint.AddFrom(1950);
yearFacetContraint.AddInterval(1980, 2012);
var yearFacet = new Facet { Name = "year", FacetContraint = yearFacetContraint };
var liFacet = new List<Facet> { genreFacet, yearFacet };
//build boolean query
var bQuery = new BooleanQuery();
var gCondition = new StringBooleanCondition("genre", "Sci-Fi");
var yCondition = new IntBooleanCondition("year");
yCondition.SetInterval(2000,2004);
bQuery.Conditions.Add(gCondition);
bQuery.Conditions.Add(yCondition);
//build search
var searchQuery = new SearchQuery<Movie> {Keyword = "star wars", Facets = liFacet, Size = 20, BooleanQuery = bQuery};
//search
var found = cloudSearch.Search(searchQuery);
SearchQuery accepts parameter Size for the number of results. But also accepts a parameter Start which can by used for paginating the results. The total number of results is also displayed in the search results: found.hits.found
You can also request the top facet as part of the results. Facet object accepts a TopResult parameter. For exemple we want the top 3 movie genres of our search var genreFacet = new Facet { Name = "genre", TopResult = 2};
- Json.NET
Developed by Martin Magakian [email protected]
MIT License (MIT)