I have got some problems to filter my queries on field Guid. Here a sample of my code. What did I miss?
public class myObject
{
public Guid Id {get;set}
public String Field1 { get; set; }
public String Field2 { get; set; }
public String Fieldn { get; set; }
public ReadingRightsEnum ReadingRights { get; set; }
public Guid UserId { get; set; }
}
// Index fct example
public void IndexMyObject(obj)
{
var result = await myClient.IndexAsync(obj, i => i
.Index("myIndexName")
.Type("myObject")
.Id(obj.Id.ToString())
.Refresh());
}
// Index fct example
public void SearchOnlyInMyUserObject(string userQuery, Guid userId)
{
var searchResult = await myClient.SearchAsync<myObject>(body =>
body.Query(q =>
q.QueryString(qs => qs.MinimumShouldMatchPercentage(100).Query(userQuery))
&& q.Term(i => i.UserId, userId))
.Fields(f => f.Id)
.Size(200));
}
// Index fct example with filter
public void SearchOnlyInMyUserObject(string userQuery, Guid userId)
{
var filters = new List<FilterContainer>
{
new FilterDescriptor<myObject>().Bool(b => b.Must(m => m.Term(i => i.UserId, userId)));
};
var searchResult = await myClient.SearchAsync<myObject>(body =>
body
.Filter(f => f.And(filters.ToArray()))
.Query(q =>
q.QueryString(qs => qs.MinimumShouldMatchPercentage(100).Query(userQuery)))
.Fields(f => f.Id)
.Size(200));
}
Both functions work fine if I filter on others parameters but return nothing when I filter on Guid. Should a convert my Guid to string when I index my object?
If i do http://xxxxxxx:9200/_search?q=userId:e4aec7b4-c400-4660-a09e-a0ce064f612e it's work fine.
Any ideas?
Thanks by advance
Edit 06/12 here a sample of myindex:
myIndexName":{
"mappings":{
"myObject":{
"properties":{
"readingrights":{
"type":"integer"
},
"id":{
"type":"string"
},
"field1":{
"type":"string"
},
"field2":{
"type":"string"
},
"userId":{
"type":"string"
}
}
}
}
}
GUID field is tricky in Elastic. If you use analyze function of elastic client it will show you how it breaks up a GUID.
AnalyzeRequest obj = new AnalyzeRequest(_index, item);
_client.Analyze(obj);
When you create an entity, You need to define guid as not be analyzed.
[String(Index = FieldIndexOption.NotAnalyzed)]
public Guid TextAssetId
I didn't fine for now why Guid got problems... but I fine a poor alternative way for now:
Instead of :
q.QueryString(qs => qs.MinimumShouldMatchPercentage(98).Query(userQuery))
&& q.Term(i => i.UserId, userId)
I do a double QueryString:
q.QueryString(qs => qs.MinimumShouldMatchPercentage(98).Query(userQuery))
&& q.QueryString(qs => qs.MinimumShouldMatchPercentage(100).Query(" \"" + userId+ "\""))
Related
I am trying to filter a list of objects by multiple criteria. First of all, if the Name and Code are the same for two or more records, I only want to return one. However, if the Status of one is "suspended" and the Status of the second is "rented", I only want to return the record with "suspended" status. This is what I've got so far:
public class Customer
{
public string UnitName { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Status { get; set; }
}
public static IEnumerable<Customer> FilterDuplicates(IEnumerable<Customer> customers)
{
return customers
.GroupBy(c => new {c.Name, c.Code})
.Select(g => g.First());
}
For example, the following data should return Mister Twister, Frank Furter and the record where the status is "suspended" for Jane Jones:
{
"unitName": "A22",
"code": "00122",
"status": "rented",
"phone": "2125551212",
"name": "Jones, Jane"
},
{
"unitName": "A07",
"code": "00122",
"status": "suspended",
"phone": "2125551212",
"name": "Jones, Jane"
},
{
"unitName": "C19",
"code": "00222",
"status": "suspended",
"phone": "2125557777",
"name": "Furter, Frank"
},
{
"unitName": "B14",
"code": "00333",
"status": "rented",
"phone": "2125559999",
"name": "Twister, Mister"
}
You should order the contents of each group and then you can select which one you want:
return customers
.GroupBy(c => new {c.Name, c.Code})
.Select(g => g.OrderByDescending(c => c.status).First());
NOTE: This is essentially a particular implementation of DistinctBy, which in full general form I implement with:
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> src, Func<T, TKey> keySelector, Func<IGrouping<TKey, T>, T> pickFn, IEqualityComparer<TKey> comparer = null) =>
src.GroupBy(keySelector, comparer).Select(pickFn);
Which you could then use as
return customers.DistinctBy(c => new { c.Name, c.Code }),
g => g.OrderByDescending(c => c.status).First());
Here is a query that works in ElasticSearch.
"query":{
"match_all":{
}
},
"size":20,
"aggs":{
"CompanyName.raw":{
"terms":{
"field":"CompanyName.raw",
"size":20,
"order":{
"_count":"desc"
}
}
}
}
}
The response from ElasticSearch has a property aggregations['CompanyName.raw']['buckets'] which is an array.
I use this code to exeute the same query via NEST
string responseJson = null;
ISearchResponse<ProductPurchasing> r = Client.Search<ProductPurchasing>(rq);
using (MemoryStream ms = new MemoryStream())
{
Client.RequestResponseSerializer.Serialize<ISearchResponse<ProductPurchasing>>(r, ms);
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
responseJson = sr.ReadToEnd();
}
}
However, in the resulting responseJson this array is always empty.
Where hs it gone?
How can I get it back?
Or is it that NEST doesn't support aggregates?
NEST does support aggregation, you can have a look into docs on how to handle aggregation response with NEST help.
Here you can find a short example of writing and retrieving data from simple terms aggregation:
class Program
{
public class Document
{
public int Id { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
public string Category { get; set; }
public override string ToString() => $"Id: {Id} Name: {Name} Brand: {Brand} Category: {Category}";
}
static async Task Main(string[] args)
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);
connectionSettings.DefaultIndex("documents");
var client = new ElasticClient(connectionSettings);
var deleteIndexResponse = await client.Indices.DeleteAsync("documents");
var createIndexResponse = await client.Indices.CreateAsync("documents", d => d
.Map(m => m.AutoMap<Document>()));
var indexDocument = await client
.IndexDocumentAsync(new Document {Id = 1, Brand = "Tommy", Category = "men"});
var indexDocument2 = await client
.IndexDocumentAsync(new Document {Id = 2, Brand = "Diesel", Category = "men"});
var indexDocument3 = await client
.IndexDocumentAsync(new Document {Id = 3, Brand = "Boss", Category = "men"});
var refreshAsync = client.Indices.RefreshAsync();
var searchResponse = await client.SearchAsync<Document>(s => s
.Query(q => q.MatchAll())
.Aggregations(a => a
.Terms("brand", t => t
.Field(f => f.Brand.Suffix("keyword")))));
var brands = searchResponse.Aggregations.Terms("brand");
foreach (var bucket in brands.Buckets)
{
Console.WriteLine(bucket.Key);
}
}
}
Prints:
Boss
Diesel
Tommy
Hope that helps.
I am using MongoDB to store all the events in my Eventbrite clone. So I have a collection called events then the objects in this collection consists of their name and and array of users that have rsvp to the event. I can query for any events that the current user has created but unable to figure out how to query both events the user has created and rsvp to.
Here is the compiled query that I am using to try to get all the users events.
events.find({"$and":[{"user_id":"5d335704802df000076bad97"},{"user_id":{"$ne":null}}],"$or":[{"checkins.user_id":"5d335704802df000076bad97"}]},{"typeMap":{"root":"array","document":"array"}})
I am using the Laravel MongoDB plugin to query my data in php it looks like this
$user->events()->orWhere(function ($query) use ($user){
return $query->where('checkins.user_id',new ObjectID($user->id));
})->get()
The event object looks something like this:
{
"name": "test",
"user_id": "1"
"rsvp": [
{"user_id": "12"}
]
}
An user can rsvp to other event that are not their own.
you need an $or filter and $elemMatch to get events that belong to a given user or events they've rsvp'd to.
db.events.find({
"$or": [
{
"user_id": "5d33e732e1ea9d0d6834ef3d"
},
{
"rsvp": {
"$elemMatch": {
"user_id": "5d33e732e1ea9d0d6834ef3d"
}
}
}
]
})
unfortunately i can't help you with laravel version of the query. in case it helps, below is the c# code that generated the above mongo query.
using MongoDB.Entities;
using System.Linq;
namespace StackOverflow
{
public class Program
{
public class user : Entity
{
public string name { get; set; }
}
public class Event : Entity
{
public string name { get; set; }
public string user_id { get; set; }
public rsvp[] rsvp { get; set; }
}
public class rsvp
{
public string user_id { get; set; }
}
private static void Main(string[] args)
{
new DB("test");
var mike = new user { name = "mike" };
var dave = new user { name = "dave" };
mike.Save();
dave.Save();
(new[] {
new Event
{
name = "mike's event",
user_id = mike.ID,
rsvp = new[]
{
new rsvp { user_id = dave.ID }
}
},
new Event
{
name = "dave's event",
user_id = dave.ID,
rsvp = new[]
{
new rsvp { user_id = mike.ID }
}
}
}).Save();
var result = DB.Find<Event>()
.Many(e =>
e.user_id == mike.ID ||
e.rsvp.Any(r => r.user_id == mike.ID));
}
}
}
From documentation here:
If the configuration of an endpoint changes, or if a message is
mistakenly sent to an endpoint, it is possible that a message type is
received that does not have any connected consumers. If this occurs,
the message is moved to a _skipped queue (prefixed by the original
queue name). The original message content is retained, and additional
headers are added to indicate the host which moved the message.
Should I expect the same behavior for a saga when some message tried to be delivered to the saga but saga instance is not created yet?
In other words, if want to handle message in saga but that message is not a trigger for saga creation. I would expect to see that messages in _skipped queue, but in reality I don't see them either in _skipped or _error queues. Also I can see that the massage successfully was delivered to the saga queue and successfully was consumed somehow without any warnings or errors. Who has consumed the message?
UPDATED
State machine:
public class TestState : SagaStateMachineInstance, IVersionedSaga
{
public Guid CorrelationId { get; set; }
public Guid Id { get; set; }
public string CurrentState { get; set; }
public int Version { get; set; }
public DateTime TimeStart { get; set; }
public int Progress { get; set; }
}
public class TestStateMachine : MassTransitStateMachine<TestState>
{
public TestStateMachine()
{
InstanceState(x => x.CurrentState);
Event(() => ProcessingStarted, x => x.CorrelateById(context => context.Message.Id));
Event(() => ProgressUpdated, x => x.CorrelateById(context => context.Message.Id));
Event(() => ProcessingFinished, x => x.CorrelateById(context => context.Message.Id));
Initially(
When(ProcessingStarted)
.Then(context =>
{
context.Instance.TimeStart = DateTime.Now;
})
.TransitionTo(Processing)
);
During(Processing,
When(ProgressUpdated)
.Then(context =>
{
context.Instance.Progress = context.Data.Progress;
}),
When(ProcessingFinished)
.TransitionTo(Processed)
.ThenAsync(async context =>
{
await context.Raise(AllDone);
})
);
During(Processed,
When(AllDone)
.Then(context =>
{
Log.Information($"Saga {context.Instance.Id} finished");
})
.Finalize());
SetCompletedWhenFinalized();
}
public State Processing { get; set; }
public State Processed { get; set; }
public Event AllDone { get; set; }
public Event<ProcessingStarted> ProcessingStarted { get; set; }
public Event<ProgressUpdated> ProgressUpdated { get; set; }
public Event<ProcessingFinished> ProcessingFinished { get; set; }
}
public interface ProcessingStarted
{
Guid Id { get; }
}
public interface ProgressUpdated
{
Guid Id { get; }
int Progress { get; }
}
public interface ProcessingFinished
{
Guid Id { get; }
}
Configuration
var repository = new MongoDbSagaRepository<TestState>(Environment.ExpandEnvironmentVariables(configuration["MongoDb:ConnectionString"]), "sagas");
var services = new ServiceCollection();
services.AddSingleton(context => Bus.Factory.CreateUsingRabbitMq(x =>
{
IRabbitMqHost host = x.Host(new Uri(Environment.ExpandEnvironmentVariables(configuration["MassTransit:ConnectionString"])), h => { });
x.ReceiveEndpoint(host, "receiver_saga_queue", e =>
{
e.StateMachineSaga(new TestStateMachine(), repository);
});
x.UseRetry(r =>
{
r.Incremental(10, TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(50));
r.Handle<MongoDbConcurrencyException>();
r.Handle<UnhandledEventException>();
});
x.UseSerilog();
}));
var container = services.BuildServiceProvider();
var busControl = container.GetRequiredService<IBusControl>();
busControl.Start();
Later, when I publish ProgressUpdated event
busControl.Publish<ProgressUpdated>(new
{
Id = id,
Progress = 50
});
I would expect it will raise UnhandledEventException and move the massage to the _error queue but in reality I can see that message appeared in the queue, consumed somehow and I don't see it in either _error or _skipped queues. MongoDB Saga storage also doesn't have any new saga instances created.
What am I doing wrong? I need to configure saga the way that if it get ProgressUpdated event when the saga instance is not created it would be retried later when the instance is ready to accept it.
SOLUTION
Reconfigure the event ProgressUpdated and ProcessingFinished as
Event(() => ProgressUpdated, x =>
{
x.CorrelateById(context => context.Message.Id);
x.OnMissingInstance(m => m.Fault());
});
Event(() => ProcessingFinished, x =>
{
x.CorrelateById(context => context.Message.Id);
x.OnMissingInstance(m => m.Fault());
});
and catching SagaException in UseRetry later
r.Handle<SagaException>();
did the work!
Thanks #Alexey Zimarev for sharing the knowledge!
If a message should be handled by a saga but there is no matching instance, the OnMissingInstance callback is called.
Reconfigure ProgressUpdated and ProcessingFinished:
Event(() => ProgressUpdated, x =>
{
x.CorrelateById(context => context.Message.Id);
x.OnMissingInstance(m => m.Fault());
});
Event(() => ProcessingFinished, x =>
{
x.CorrelateById(context => context.Message.Id);
x.OnMissingInstance(m => m.Fault());
});
and catch SagaException in the retry filter:
r.Handle<SagaException>();
I am using the latest version of jQuery and ASP.NET MVC 3 with the Razor view engine.
I have tried Google looking for a decent example of loading a child drop down list when a parent drop down item is selected. I am looking to do this via jQuery AJAX using JSON. My knowledge of this is zero.
I have a Category class with a list of categories. It's a parent-child association.
If I select a category from the parent drop down list, then all the child categories need to be listed in the child drop down list for the selected parent category.
This is what I currently have, but need to complete it, not sure if I am in the right direction:
$(document).ready(function () {
$('#ddlParentCategories').change(function () {
alert('changed');
});
});
I loaded my drop down list from my view model as such:
#Html.DropDownListFor(x => x.ParentCategoryId, new SelectList(Model.ParentCategories, "Id", "Name", Model.ParentCategoryId), "-- Select --", new { id = "ddlParentCategories" })
The first item has text "-- Select --" (for both parent and child drop down lists). On initial page load nothing must be loaded in the child drop down list. When a value is selected then the child drop down list must be populated. And when "-- Select --" is selected again in the parent drop down list then all the items in the child drop down list must cleared except "-- Select --".
If possible, if the child categories is loading, how do I display that "round" loading icon?
UPDATE
I have updated my code to Darin's code, and I cannot get it to work properly:
Category class:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public bool IsActive { get; set; }
public int? ParentCategoryId { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual ICollection<Category> ChildCategories { get; set; }
}
EditProductViewModel class:
public class EditProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public bool IsActive { get; set; }
public string PageTitle { get; set; }
public bool OverridePageTitle { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public int ParentCategoryId { get; set; }
public IEnumerable<Category> ParentCategories { get; set; }
public int ChildCategoryId { get; set; }
public IEnumerable<Category> ChildCategories { get; set; }
}
ProductController class:
public ActionResult Create()
{
EditProductViewModel viewModel = new EditProductViewModel
{
ParentCategories = categoryService.GetParentCategories()
.Where(x => x.IsActive)
.OrderBy(x => x.Name),
ChildCategories = Enumerable.Empty<Category>(),
IsActive = true
};
return View(viewModel);
}
public ActionResult AjaxBindingChildCategories(int parentCategoryId)
{
IEnumerable<Category> childCategoryList = categoryService.GetChildCategoriesByParentCategoryId(parentCategoryId);
return Json(childCategoryList, JsonRequestBehavior.AllowGet);
}
Create view:
<tr>
<td><label>Parent Category:</label> <span class="red">*</span></td>
<td>#Html.DropDownListFor(x => x.ParentCategoryId,
new SelectList(Model.ParentCategories, "Id", "Name", Model.ParentCategoryId),
"-- Select --",
new { data_url = Url.Action("AjaxBindingChildCategories"), id = "ParentCategories" }
)
#Html.ValidationMessageFor(x => x.ParentCategoryId)
</td>
</tr>
<tr>
<td><label>Child Category:</label> <span class="red">*</span></td>
<td>#Html.DropDownListFor(x => x.ChildCategoryId,
new SelectList(Model.ChildCategories, "Id", "Name", Model.ChildCategoryId),
"-- Select --",
new { id = "ChildCategories" }
)
#Html.ValidationMessageFor(x => x.ChildCategoryId)
</td>
</tr>
<script type="text/javascript">
$(document).ready(function () {
$('#ParentCategories').change(function () {
var url = $(this).data('url');
var data = { parentCategoryId: $(this).val() };
$.getJSON(url, data, function (childCategories) {
var childCategoriesDdl = $('#ChildCategories');
childCategoriesDdl.empty();
$.each(childCategories, function (index, childCategory) {
childCategoriesDdl.append($('<option/>', {
value: childCategory, text: childCategory
}));
});
});
});
});
</script>
It goes into my AjaxBindingChildCategories action and it brings back records, it just doesn't want to display my child category dropdownlist. I had a look in Fire Bug and the error that I get is:
GET AjaxBindingChildCategories?parentCategoryId=1
500 Internal Server Error
Here's an example of cascading drop down lists. As always start by defining a view model:
public class MyViewModel
{
[DisplayName("Country")]
[Required]
public string CountryCode { get; set; }
public IEnumerable<SelectListItem> Countries { get; set; }
public string City { get; set; }
public IEnumerable<SelectListItem> Cities { get; set; }
}
then a controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
// TODO: Fetch countries from somewhere
Countries = new[]
{
new SelectListItem { Value = "FR", Text = "France" },
new SelectListItem { Value = "US", Text = "USA" },
},
// initially we set the cities ddl to empty
Cities = Enumerable.Empty<SelectListItem>()
};
return View(model);
}
public ActionResult Cities(string countryCode)
{
// TODO: based on the selected country return the cities:
var cities = new[]
{
"Paris", "Marseille", "Lyon"
};
return Json(cities, JsonRequestBehavior.AllowGet);
}
}
a view:
#model MyViewModel
#using (Html.BeginForm())
{
<div>
#Html.LabelFor(x => x.CountryCode)
#Html.DropDownListFor(
x => x.CountryCode,
Model.Countries,
"-- Select country --",
new { data_url = Url.Action("cities") }
)
#Html.ValidationMessageFor(x => x.CountryCode)
</div>
<div>
#Html.LabelFor(x => x.City)
#Html.DropDownListFor(
x => x.City,
Model.Cities,
"-- Select city --"
)
#Html.ValidationMessageFor(x => x.City)
</div>
<p><input type="submit" value="OK" /></p>
}
and finally the unobtrusive javascript in a separate file:
$(function () {
$('#CountryCode').change(function () {
var url = $(this).data('url');
var data = { countryCode: $(this).val() };
$.getJSON(url, data, function (cities) {
var citiesDdl = $('#City');
citiesDdl.empty();
$.each(cities, function (index, city) {
citiesDdl.append($('<option/>', {
value: city,
text: city
}));
});
});
});
});
jQuery script will look like this:
<script type="text/javascript">
function getCities(abbr) {
$.ajax({
url: "#Url.Action("Cities", "Locations")",
data: {abbreviation: abbr},
dataType: "json",
type: "POST",
error: function() {
alert("An error occurred.");
},
success: function(data) {
var items = "";
$.each(data, function(i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#City").html(items);
}
});
}
$(document).ready(function(){
$("#State").change(function() {
var abbr = $("#State").val();
getCities(abbr);
});
});
</script>
A repository to retrieve data could look like this (obviously hook it up to live data though):
public class LocationRepository : ILocationRepository
{
public IQueryable<State> GetStates()
{
return new List<State>
{
new State { Abbreviation = "NE", Name = "Nebraska" },
new State { Abbreviation = "NC", Name = "North Carolina" }
}.AsQueryable();
}
public IQueryable<City> GetCities(string abbreviation)
{
var cities = new List<City>();
if (abbreviation == "NE")
{
cities.AddRange(new List<City> {
new City { Id = 1, Name = "Omaha" },
new City { Id = 2, Name = "Lincoln" }
});
}
else if (abbreviation == "NC")
{
cities.AddRange(new List<City> {
new City { Id = 3, Name = "Charlotte" },
new City { Id = 4, Name = "Raleigh" }
});
}
return cities.AsQueryable();
}
}
public interface ILocationRepository
{
IQueryable<State> GetStates();
IQueryable<City> GetCities(string abbreviation);
}
Controllers could look like this:
public class LocationsController : Controller
{
private ILocationRepository locationRepository = new LocationRepository();
[HttpPost]
public ActionResult States()
{
var states = locationRepository.GetStates();
return Json(new SelectList(state, "Id", "Name"));
}
[HttpPost]
public ActionResult Cities(string abbreviation)
{
var cities = locationRepository.GetCities(abbreviation);
return Json(new SelectList(cities, "Abbreviation", "Name"));
}
}
I am assuming you populate the parent dropdown from server side and also the first option in this parent dd is "--- Select ---"
You can try this
$(document).ready(function () {
var $parent = $('#ddlParentCategories');
var $child = $('#ddlChildCategories');
$child.find("option:gt(0)").remove();
if(!$parent.children().eq(0).is(":selected")){
$parent.change(function () {
$.ajax({
url: "urlToFetchTheChild",
data: { categoryId: this.value },
success: function(data){
//The data you send should be a well formed array of json object containing code/value pair of child categories
for(var i = 0;i<data.length;i++){
$child.append("<option value='"+ data[i].code +"'>"+ data[i].value +"</option>");
}
}
});
});
}
});