Elastic Search Fetch Exact document - c#

I've been trying to fetch document from elastic search on the basis of two fields "sAMAccountName" and "Container" but no luck.
enter image description here
What i want is a document where both attributes are exactly equally to my given value.
This is something I've been doing
It gave me results where user contain's the given property but i want exact match.
string container = getUserContainer(identityStore.ConnectionString);
var searchRequest = new Nest.SearchRequest();
searchRequest.Size = 10000;
searchRequest.Query = GetQuery(knownAttributes.SamAccountName, userName) && GetQuery("Container", container);
var searchResults = elasticSearch.Search<Dictionary<string,object>>(searchRequest);
return null;
private Nest.MatchQuery GetQuery(Nest.Field field, string query)
{
var matchQuery = new Nest.MatchQuery();
matchQuery.Field = field;
matchQuery.Query = query;
matchQuery.Operator = Operator.And;
r
eturn matchQuery;
}

Try this:
IQueryResponse<T> result = client.Search<T>(s => s
.Size(1)
.Query(q => q.Term("field1", value1) && q.Term("field2", value2))
.Index("myindex")
.Type("mytype")
);
if (result.Documents.Count<T>() > 0)
{
//do something
}

Related

AWS .NET SDK DynamoDB Filter expression by Map values doesn't return any data

I'm trying to load data from DynamoDB.
I use FilterExpression and KeyExpression.
If I search by simple value on the top level everything works fine.
However, when I try to filter records by nested map values, I get 0 records.
CurrentCase is an object, Assignments is Dictionary, Setup is Enum.
Here is my code:
`Expression filterExpression = new ();
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments.Setup";
filterExpression.ExpressionAttributeValues[":userId"] = userId;
filterExpression.ExpressionStatement = "#Setup = :userId";`
I tried another way, didn't help. (WHERE CurrentCase.Assignments['Setup'] = 'Id' works in PartyQL):
`Expression filterExpression = new ();
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments['Setup']";
filterExpression.ExpressionAttributeValues[":userId"] = userId;
filterExpression.ExpressionStatement = "#Setup = :userId";`
This is how i call query
var queryOperationConfig = new QueryOperationConfig
{
PaginationToken = paginationToken,
Limit = pageSize,
IndexName = GlobalIndexNames.Cases,
KeyExpression = keyExpression,
FilterExpression = filterExpression
};
Search search = _dbContext.GetTargetTable<CaseEntity>().Query(queryOperationConfig);
List<Document> documents = await search.GetNextSetAsync(cancellationToken);
I expect that this request return all records where CurrentCase.Assignments['Setup'] equals userId
Forgive me, im not a .Net coder, but your issue is this:
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments.Setup";
You are essentially setting your var #Setup to a String "CurrentCase.Assignments.Setup"
It should be:
ExpressionAttributeNames = new Dictionary<string, string>
{
{ "#CurrentCase", "CurrentCase" },
{ "#Assignments", "Assignments" },
{ "#Setup", "Setup" }
}
filterExpression.ExpressionStatement = "#CurrentCase.#Assignments#Setup = :userId";`
You may need to restructure the example I gave, but you should get the idea.

Select multiple times using LINQ based on result

I currently have this dataset
This is the current dataset
So, I am trying to return the data based on the Category Id as an input. But if keyword is not present for the category, then the data from its parent should be returned
This is an example
Input: 202; Output: ParentCategoryID=201, Name=Operating System,
Keywords=Teaching
This is what I have right now (testData is the dataset)
public static string GetData(int categoryId)
{
var searchResult = testData.Where(x => x.CategoryId == categoryId).FirstOrDefault();
//if has no keywords return parent data
if(string.IsNullOrEmpty(searchResult.Keywords))
{
var parentData = testData.Where(x => x.CategoryId == searchResult.ParentCategoryId);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("ParentCategoryId=");
stringBuilder.Append(parentData.ParentCategoryId);
stringBuilder.Append(", Name=");
stringBuilder.Append(parentData.Name);
stringBuilder.Append(", Keywords=");
stringBuilder.Append(parentData.Keywords);
return stringBuilder.ToString();
}
But this only works with just one parent. It works in this kind of scenarios
Input: 201; Output: ParentCategoryID=200, Name=Computer,
Keywords=Teaching
I think there is an issue with my LINQ query, I would appreciate any advice
A simple while loop might be what you need
int input = 202;
bool flag = true;
var result = datalist.Where(q => q.CatId == input).FirstOrDefault();
var parrent = datalist.Where(q => q.CatId == result.ParrentId).FirstOrDefault();
var keyword = "";
while (String.IsNullOrEmpty(parrent.Keyword))
{
parrent = datalist.Where(q => q.CatId == parrent.ParrentId).FirstOrDefault();
}
keyword = parrent.Keyword;
Get the parent via a recursive function:
public static WhateverYourDataTypeIs GetParent(int parentCategoryId)
var parentData = testData.Where(x => x.CategoryId == parentCategoryId).FirstOrDefault();
if(string.IsNullOrEmpty(parentData.Keywords))
{
return GetParent(parentData.ParentCategoryId);
}
return parentData;
}
And just call that instead of the old way you got the parent
// OLD:
var parentData = testData.Where(x => x.CategoryId == searchResult.ParentCategoryId).FirstOrDefault();
// NEW:
var parentData = GetParent(searchResult.ParentCategoryId)

How do you reuse mapping functions on Nested entities in Entity Framework?

I have seen multiple questions that are similar to this one but I think my case is slightly different. I'm using EF6 to query the database and I'm using data projection for better queries.
Given that performance is very important on this project I have to make sure to just read the actual fields that I will use so I have very similar queries that are different for just a few fields as I have done this I have noticed repetition of the code so I'm been thinking on how to reuse code this is currently what I Have:
public static IEnumerable<FundWithReturns> GetSimpleFunds(this DbSet<Fund> funds, IEnumerable<int> fundsId)
{
IQueryable<Fund> query = GetFundsQuery(funds, fundsId);
var results = query
.Select(f => new FundWithReturns
{
Category = f.Category,
ExpenseRatio = f.ExpenseRatio,
FundId = f.FundId,
Name = f.Name,
LatestPrice = f.LatestPrice,
DailyReturns = f.FundDailyReturns
.Where(dr => dr.AdjustedValue != null)
.OrderByDescending(dr => dr.CloseDate)
.Select(dr => new DailyReturnPrice
{
CloseDate = dr.CloseDate,
Value = dr.AdjustedValue.Value,
}),
Returns = f.Returns.Select(r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
}).FirstOrDefault()
})
.ToList();
foreach (var result in results)
{
result.DailyReturns = result.DailyReturns.ConvertClosingPricesToDailyReturns();
}
return results;
}
public static IEnumerable<FundListVm> GetFundListVm(this DbSet<Fund> funds, string type)
{
return funds
.Where(f => f.StatusCode == MetisDataObjectStatusCodes.ACTIVE
&& f.Type == type)
.Select(f => new FundListVm
{
Category = f.Category,
Name = f.Name,
Symbol = f.Symbol,
Yield = f.Yield,
ExpenseRatio = f.ExpenseRatio,
LatestDate = f.LatestDate,
Returns = f.Returns.Select(r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
}).FirstOrDefault()
}).OrderBy(f=>f.Symbol).Take(30).ToList();
}
I'm trying to reuse the part where I map the f.Returns so I tried created a Func<> like the following:
private static Func<Return, ReturnValues> MapToReturnValues = r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
};
and then use like this:
public static IEnumerable<FundListVm> GetFundListVm(this DbSet<Fund> funds, string type)
{
return funds
.Where(f => f.StatusCode == MetisDataObjectStatusCodes.ACTIVE
&& f.Type == type)
.Select(f => new FundListVm
{
Category = f.Category,
Name = f.Name,
Symbol = f.Symbol,
Yield = f.Yield,
ExpenseRatio = f.ExpenseRatio,
LatestDate = f.LatestDate,
Returns = f.Returns.Select(MapToReturnValues).FirstOrDefault()
}).OrderBy(f=>f.Symbol).Take(30).ToList();
}
The compiler is ok with it but at runtime, it crashes and says: Internal .NET Framework Data Provider error 1025
I tried to convert the Func into Expression like I read on some questions and then using compile() but It didn't work using AsEnumerable is also not an option because It will query all the fields first which is what I want to avoid.
Am I trying something not possible?
Thank you for your time.
It definitely needs to be Expression<Func<...>>. But instead of using Compile() method (not supported), you can resolve the compile time error using the AsQueryable() method which is perfectly supported (in EF6, the trick doesn't work in current EF Core).
Given the modified definition
private static Expression<Func<Return, ReturnValues>> MapToReturnValues =
r => new ReturnValues { ... };
the sample usage would be
Returns = f.Returns.AsQueryable().Select(MapToReturnValues).FirstOrDefault()

Search form has an Enum dropdown field whose value may be null (none selected) once it reaches my lambda Linq query

Basically, if the user selected no option from the dropdown combo, I want it to be left out from my Linq query that looks something like this:
// this is how I manage the form post data, please
// propose a better way if you know one
Dictionary<string, string> formdata = new Dictionary<string, string>();
foreach(string key in Request.Form.AllKeys)
{
formdata.Add(key, Request.Form[key]);
}
// getting the title
string title = "";
formdata.TryGetValue("postedTitle", out title);
// getting the level
string levelString = "";
formdata.TryGetValue("postedLevel", out levelString );
int level = -1;
if(levelString != "")
{
Int32.TryParse(levelString , out level);
}
var model = new FooIndexVM
{
Foos = _ctx.SomeDbSet.Where(w => w.Title.Contains(title) && w.Level == (Level?)level.Value).Select(x => new FooBarRow
{
FooBarId = x.Id,
....
Since I'm getting either 0 or -1 for the level -- I need a way to gracefully leave the Enum part from the query completely. I will also later add some additional fields similar to this one (may be unselected) so the solution will also work for those, I guess.
You can chain Where commands so this line:
Foos = _ctx.SomeDbSet.Where(w => w.Title.Contains(title) && w.Level == (Level?)level.Value).Select(x => new FooBarRow
{
FooBarId = x.Id,
....
Could be rewritten to be this without changing its behaviour (multiple Wheres effectively become combined with &&s):
Foos = _ctx.SomeDbSet.Where(w => w.Title.Contains(title)).Where(w => w.Level == (Level?)level.Value).Select(x => new FooBarRow
{
FooBarId = x.Id,
....
This then means that you can add some logic around whether to apply the second Where or not like this, for example:
var query = _ctx.SomeDbSet.Where(w => w.Title.Contains(title));
if (level != -1)
{
query = query.Where(w => w.Level == (Level?)level.Value)
}
Foos = query.Select(x => new FooBarRow
{
FooBarId = x.Id,

Retrieve single record to model with EF Linq, does it need to loop to populate?

Most of the time I retrieve multiple records so I would end up doing this
var rpmuser = new List<rpm_scrty_rpm_usr>();
I have my List collection of properties from poco
So I typically use select new in my Linq statement
Then I use a foreach and loop over the records in which the List would get model.Add(new instance in each loop)
However , do I really need to be doing all this looping to populate?
Bigger question when i have a single record should I be needing to even do a loop at all?
public bool UpdateAllUsers(string user, string hash, string salt)
{
bool status = false;
var rpmuser = new rpm_scrty_rpm_usr();
var query = (from t in db.rpm_usr
.Where(z => z.usr_id == "MillXZ")
select new
{
t.usr_id,
t.usr_lnm,
t.usr_pwd,
t.usr_fnm,
t.salt,
t.inact_ind,
t.lst_accs_dtm,
t.lst_pwd_chg_dtm,
t.tel,
t.wwid,
t.email_id,
t.dflt_ste_id,
t.apprvr_wwid,
t.chg_dtm,
t.chg_usr_id,
t.cre_dtm,
t.cre_usr_id,
});
foreach(var s in query)
{
rpmuser.wwid = s.wwid;
rpmuser.usr_pwd = s.usr_pwd;
rpmuser.usr_lnm = s.usr_lnm;
rpmuser.usr_id = s.usr_id;
rpmuser.usr_fnm = s.usr_fnm;
rpmuser.tel = s.tel;
rpmuser.salt = s.salt;
rpmuser.lst_pwd_chg_dtm = rpmuser.lst_pwd_chg_dtm;
rpmuser.lst_accs_dtm = s.lst_accs_dtm;
rpmuser.inact_ind = s.inact_ind;
rpmuser.email_id = s.email_id;
rpmuser.apprvr_wwid = s.apprvr_wwid;
rpmuser.chg_dtm = s.chg_dtm;
rpmuser.chg_usr_id = s.chg_usr_id;
rpmuser.cre_usr_id = s.cre_usr_id;
rpmuser.dflt_ste_id = s.dflt_ste_id;
rpmuser.cre_dtm = s.cre_dtm;
}
DateTime dateTime = DateTime.Now;
try
{
rpmuser = db.rpm_usr.Find(rpmuser.usr_id);
rpmuser.usr_pwd = hash;
rpmuser.salt = salt;
db.SaveChanges();
status = true;
}
catch (Exception ex)
{
status = false;
}
return status;
}
I am not exactly sure what you want. Your method says Update All, but only seems to be attempting to update one record. So why don't you just do this?
try
{
var rpmuser = db.rpm_usr.Single(z => z.usr_id == "MillXZ");
rpmuser.usr_pwd = hash;
rpmuser.salt = salt;
db.SaveChanges();
status = true;
}
catch (Exception ex)
{
status = false;
}
You have a lot of redundant declarations unless I am missing something. In the case of the list you will do something like this:
var query = db.rpm_usr.Where(z => z.usr_id == "...some string...");
foreach(var item in query)
{
rpmuser.usr_pwd = ...some value...;
rpmuser.salt = ...some value...;
}
db.SaveChanges();
I can't stress this enough, Murdock's answer is absolutely the right way to fix the code you've shown. You are writing way too much code for what you're trying to accomplish.
However, to answer your question about whether you need to loop in other situations, you can get away from having to loop by doing the projection into a new type as part of your LINQ-to-Entities query. The looping still happens, you just don't see it.
var query = db.rpm_usr
.Where(z => z.usr_id == "MillXZ")
.AsEnumerable()
.Select(z => new rpm_scrty_rpm_usr()
{
usr_id = z.usr_id,
usr_lnm = z.usr_lnm,
// etc...
});
You would then finish the query off with a .Single(), .SingleOrDefault(), or .ToList() depending on whether you expected exactly one, one or zero, or a list. For example, in this case if you might find one or zero users with the name "MillXZ" you would write the following.
var query = db.rpm_usr
.Where(z => z.usr_id == "MillXZ")
.AsEnumerable()
.Select(z => new rpm_scrty_rpm_usr()
{
usr_id = z.usr_id,
usr_lnm = z.usr_lnm,
// etc...
})
.SingleOrDefault();

Categories

Resources