Skip to content

Instantly share code, notes, and snippets.

@markholdt
Last active January 22, 2016 10:59
Show Gist options
  • Save markholdt/61cb0751e5b5fa99b09e to your computer and use it in GitHub Desktop.
Save markholdt/61cb0751e5b5fa99b09e to your computer and use it in GitHub Desktop.
public ElasticClient Elastic { get; set; } // this is instantiated in the AppHost using IoC
public object Get(GetListings request)
{
int pageSize = 10;
//request parameters
int pageNr = request.PageNr ?? 1;
string searchText = request.SearchText ?? "*";
string city = request.City ?? "*";
string categoryId = request.CategoryId?.ToString() ?? "*";
string priceFrom = request.PriceFrom?.ToString() ?? "0";
string priceTo = request.PriceTo?.ToString() ?? "9999999999999";
long totalRecords = 0;
// creating the search request
QueryContainer query1 = new WildcardQuery
{
Field = "_all",
Value = searchText
};
QueryContainer cityFilter = new WildcardQuery
{
Field = "city",
Value = city,
};
QueryContainer categoryFilter = new WildcardQuery
{
Field = "category.id",
Value = categoryId
};
QueryContainer priceRange = new RangeQuery
{
Field = "price",
GreaterThanOrEqualTo =priceFrom,
LowerThanOrEqualTo = priceTo,
};
var searchRequest = new SearchRequest
{
From = (pageNr * pageSize),
Size = pageSize,
Query = query1 && cityFilter && categoryFilter && priceRange
};
var results = Elastic.Search<Listing>(searchRequest);
var resultListing = results.Documents;
totalRecords = results.Total;
return new GetListingsResponse { Result = resultListing, TotalRecords = totalRecords, CurrentPageNumber = pageNr, TotalPageCount = totalRecords/pageSize };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment