Using the PHP Elastica library, I'm wondering what is the best way to check whether a document with Id=1 exists?
I was doing as follows:
$docPre = $elasticaType->getDocument(1);
if ($docPre) {
//do some stuff...
} else {
//do something else...
}
However, the above code does not work because a NotFoundException is thrown by the getDocument() method if the document does not exist.
Alternatively, I could do a type "search" using something like this:
$elasticaQueryString = new \Elastica\Query\QueryString();
$elasticaQueryString->setParam('id', 1);
$elasticaQuery = new \Elastica\Query();
$elasticaQuery->setQuery($elasticaQueryString);
$resultSet = $elasticaType->search($elasticaQuery);
$count = $resultSet->count();
if ($count > 0) {
//do some stuff...
} else {
//do something else...
}
However, the above seems quite cumbersome... What's the better way?
This other question applies to ElasticSearch, and one of the answers suggests my first approach (the equivalent to using getDocument). However, I do not want an Exception to be thrown, as it would be the case using Elastica...
Rather than preventing the Exception from being thrown, one way would be to simply deal with it with a "Try, throw and catch" block like this:
try {
$docPre = $elasticaType->getDocument(1);
} catch (Exception $e) {
$docPre = NULL;
}
if ($docPre != NULL) {
//do some stuff...
} else {
//do something else...
}
Related
This is my search code please see then solution, thank you.
You have to use like this
$report_ex = Expenses::where(condition)->where(condition)->get();
or
$report_ex = Expenses::where(condition)->where(condition)->first();
If you are using
$report_ex = Expenses::where(condition)->where(condition)->first();
then you need to call
if(!empty($report_ex))
{
// your code
}
else
{
//your code
}
But if you are using
$report_ex = Expenses::where(condition)->where(condition)->get();
then you should use
if(count($report_ex) > 0)
{
// your code
}
else
{
// your code
}
since get function returns an empty object
So I have a list and for each item in the list I have to "do something" based on each list element.
The list consists of codes and there are a total of 5 codes. The list may contain any or all codes.
So far I've used the forEach and i've written the if conditions inside it as below -
List<Category> categories = getCategories();
categories.stream().forEach(category -> {
if(category.equals(Category.A)) {
// do something
} else if(category.equals(Category.B)) {
// do something
} else if(category.equals(Category.C)) {
// do something
} else if(category.equals(Category.D)) {
// do something
} else if(category.equals(Category.E)) {
// do something
}
});
I'm looking at refactoring this. Can someone please look at how better this can be done?
The only thing I would improve is to use a switch-statement:
switch(category){
case Category.A:
// do Something
break;
}
As mentionend by luk2302 this will only work if Category is an enum.
You can add a doSomething method to the Category class and simply call it in the .forEach.
For example:
public class Category {
// any other methods/variable/constructors
public void doSomething() {
//do something
}
}
Then you can call it like this:
categories.stream().forEach(Category::doSomething);
If the // do something has not a common behaviour, you can move the if part inside the doSomething method.
At first, do not use multiline lambdas and create new method (with switch):
private void doSomethingBasedOnCategory(Category category) {
switch(category) {
case A: // do something
break;
case B: // do something
break;
case C: // do something
break;
case D: // do something
break;
case E: // do something
break;
}
}
Then use it in your lambda:
getCategories()
.stream()
.forEach(category -> doSomethingBasedOnCategory(category);
Another way is to create static map prefilled with keys (which will be Category.X) and values (which will be functions ready to use)
I'm trying to get some data from a database whose results can be more than one row.
I've the following code for that:
public System.Linq.IQueryable<Users> getUser2(string idUser)
{
try
{
using (Entities c = new Entities())
{
c.ContextOptions.LazyLoadingEnabled = false;
c.ContextOptions.ProxyCreationEnabled = false;
return c.Users.Include("Empresas").Where(x => x.Login == idUser && x.Empresas.Activa == true);
}
}
catch (Exception ex)
{
throw ex;
}
}
But it doesn't seem to get any result, it shows something like a badly formed Iqueryable, I mean if I expand its results view I can see a message that says "ObjectContext instance has been eliminated and cannot be used for operations that need a connection" If I try to access any Users element with the function ElementAt(index) I get an IndexOutOfBounds error as it looks like it has no data if watched on debug mode.
I've deduced that it's Where fault because this code Works fine in returning the first user it finds that fulfills the condition:
public Users getUser(string idUser)
{
try
{
using (Entities c = new Entities())
{
c.ContextOptions.LazyLoadingEnabled = false;
c.ContextOptions.ProxyCreationEnabled = false;
return c.Users.Include("Empresas").FirstOrDefault(x => x.Login == idUser && x.Empresas.Activa == true);
}
}
catch (Exception ex)
{
throw ex;
}
}
Does that Where work differently than what I think I should? If then, how could I get several data that fulfills the conditions I'm passing the same as in getUser but for several rows?
Thanks for your attention.
You need to enumerate the result, so after the "where" statement add. ToList() which will enumerate and execute the query against your database. FirstOrDefault is executing the query thats why you get a result.
You need to check the deferred methods and understand how they work.
EDIT
The following are some links to show you the deference between the Deferred method vs Immediate methods in LINQ
1- http://www.dotnetcurry.com/showarticle.aspx?ID=750
2- http://www.codeproject.com/Articles/627081/LINQ-Deferred-Execution-Lazy-Evaluation
3- http://visualcsharptutorials.com/linq/deferred-execution
Hope that helps.
I have a table client and from retrieving results I use this way
public ClientParent getClient(Long clientId,Long parentId){
String queryString="SELECT cp FROM Client cp where cp.cid.id=:clientId " +
"and cp.pid.id=:parentId ";
Query query=entityManagerUtil.getQuery(queryString);
query.setParameter("clientId", clientId);
query.setParameter("parentId", parentId);
return (ClientParent)query.getSingleResult();
}
This is the DAO method.
Actually for getting client at 1st control goes to controller class then to service and then DAO class
Now lets say that the client table is empty so in this case return (ClientParent)query.getSingleResult(); will throw me error.
I can handle this in by wrting in try catch block in service class as well as in controller class.But wanted to know if I can do with out throwing any exception.I mean do I have change the query or what should I return so that it will never throw exception even if the table is empty
you can use the getResultList() method
public ClientParent getClient(Long clientId,Long parentId){
String queryString="SELECT cp FROM Client cp where cp.cid.id=:clientId " +
"and cp.pid.id=:parentId ";
Query query=entityManagerUtil.getQuery(queryString);
query.setParameter("clientId", clientId);
query.setParameter("parentId", parentId);
List<ClientParent> result = query.getResultList();
if (result != null && result.size() >0){
return result.get(0);
} else {
return null;
}
}
I suggest you to surround your code with try-catch block. So will sure that the data is correct.
try {
// ... your code goes here
// getSingleResult()
return XXX;
} catch(NonUniqueResultException e) {
// here you know there is some bad data
// so you can ignore it or do something
} catch(NoResultException e){
return null;
}
I am using EF4/LINQ for the first time and have run into an issue. I am looping thru the results of a LINQ query using a foreach loop as follows:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
CallOutcomeSubmission los = new CallOutcomeSubmission();
client = connectToService();
try
{
using (var context = new CallOutcomeContext())
{
// List of available actions
private static string ACTION_CALL_ATTEMPT = "Call Attempt";
DateTime oneDayAgo = DateTime.Now.AddHours(-24);
var query = from co in context.T_MMCallOutcome
join ca in context.T_Call on co.CallID equals ca.CallID
join lv in context.T_LeadVendorEmailHeader on co.LeadVendorEmailID equals lv.LeadVendorEmailID
where co.EnteredOn > oneDayAgo && co.MMLeadActionID == null
select new
{
co.CallOutcomeID,
co.CallID,
co.LeadVendorEmailID,
MMLeadID = lv.email_text,
ca.OutcomeID,
lv.FranchiseNumber,
co.MMLeadActionID,
co.LeadAction
};
// if any results found for query
if (query.Any())
{
foreach (var call in query.ToList())
{
// if the franchise exists
if (client.FranchiseExists(int.Parse(call.FranchiseNumber)))
{
switch (call.OutcomeID)
{
case 39: // Not Answered
call.LeadAction = ACTION_CALL_ATTEMPT;
break;
case 43: // Remove from Call List
break;
default: // If the OutcomeID is not identified in the case statement
break;
} // switch
}
else
{
los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent: No franchise found with franchise ID " + call.FranchiseNumber);
}
// Save any changes currently on context
context.SaveChanges();
} // foreach
}
// if no results found from query write system log stating such
else
{
los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent: No new entries found");
}
} // using
client.Close();
}
catch (System.TimeoutException exception)
{
los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent:" + exception.ToString());
client.Abort();
}
catch (System.ServiceModel.CommunicationException exception)
{
los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent:" + exception.ToString());
client.Abort();
}
}
When I try to do the assignment:
call.LeadAction = ACTION_CALL_ATTEMPT;
I get a build error of
Property or indexer 'AnonymousType#2.LeadAction' cannot be assigned to -- it is read only
I can't seem to find anything on this specific error doing a Google search and am not sure what I am doing wrong. Is it because the original query contains a join?
How can I do the assignment of call.LeadAction within the foreach loop?
I would also like to know if there are design issue withe way I have written the query or performed any of the operations since this is my first foray into EF/LINQ.
You're creating a new anonymous type - with the Linq joins and then trying to set that value. What you're really wanting to do, is update the call's LeadAction correct?
How would EF know to translate your new query back to an entity so it can go back to the database? It would have to go through alot of hoops, and it's not capable of that.
What you could do, is retrieve the Call from your database and set the LeadAction that way - I'm using Find, assuming that CallID is your PK:
case 39: // Not Answered
var thisCall = context.T_Call.Find(call.CallID)
thisCall.LeadAction = ACTION_CALL_ATTEMPT;
break;