How can I search for multiple words with Sitecore 7 ?
I tried with Contains for every word but doesn't work fine and I think performance it's not really good.
you can user PredicateBuilder for this issue :
Code will be something like:
Queryable<SearchItem> SearchText(List<string> keywords, IQueryable<SearchItem> itemList)
{
var predicate = PredicateBuilder.True<SearchItem>();
foreach (string keyword in keywords)
{
predicate = predicate.And(i => i.FieldOne.Contains(keyword) || i.FieldTwo.Contains(keyword) || i.FieldThree.Contains(keyword) || i.Fieldyyy.Contains(keyword));
}
return itemList.Where(predicate);
}
About predicateBuilder you can find here :
Dynamic query using predicate builder
Performance can get better if you use the ToList(); on the IQueryable only when you have filtered out all items (using LINQ statements) you need from the IQueryable. When you call .ToList(); the query will be executed.
Related
I have a list of records (record is a case class). I am trying to simultaneously update all of the records in the list (using the copy function) and return a list of the updated records.
I tried to use a foreach method but it's not allowing me to produce the list as a result.
This is what I tried so far, I'm not sure if any of this is right
def update_record(record: List[Rec]): List[Rec] = {
val result = record.foreach(r => {
r.copy(firstname = get_firstname(r, record), lastname = get_lastname(r, record))
})
result
}
but result doesn't produce a list so I'm not sure what to do now.
You just need to use map instead of foreach.
def update_record(record: List[Rec]: List[Rec] =
record.map { r =>
r.copy(firstname = get_firstname(r, record), lastname = get_lastname(r, record))
}
Anyways, it would be good to follow some tutorial or course or read a book about the language. This is just basic knowledge of the standard library.
Some links that may be useful:
Scala tour.
Scala book.
Scaladoc.
Scala style guide.
I have an angular app using the mongodb sdk for js.
I would like to suggest some words on a input field for the user from my words collection, so I did:
getSuggestions(term: string) {
var regex = new stitch.BSON.BSONRegExp('^' +term , 'i');
return from(this.words.find({ 'Noun': { $regex: regex } }).execute());
}
The problem is that if the user type for example Bie, the query returns a lot of documents but the most accurated are the last ones, for example Bier, first it returns the bigger words, like Bieberbach'sche Vermutung. How can I deal to return the closests documents first?
A regular-expression is probably not enough to do what you are intending to do here. They can only do what they're meant to do – match a string. They might be used to give you a candidate entry to present to the user, but can't judge or weigh them. You're going to have to devise that logic yourself.
When using a predicate, we can have:
filterPredicate = [NSPredicate predicateWithFormat:#"(ANY names.firstName contains[c] %#), nameToSearchFor];
This use of ANY allows us to find any object where any of the objects in the names collection has a firstName containing the desired text.
Is it possible to do something similar with filter expressions in Swift 3? In other words, something like:
allPeople.filter { $0.(ANY)names.firstName.contains(searchString) };
(The above ANY syntax is made up for illustration).
Perhaps could be done by nesting a reduce that concatenates all the firstNames, then see if my target string is contained in that?
allPeople.filter { $0.names.contains(where: { $0.firstName.contains(searchString) }) }
I have a one to many entity:
User -> OrderPerson
A user can own multiple orderPersons.
An orderPerson is linked to Orders and can have multiple orders.
What I want to do is build a dynamic query to deal with this, this is what I have thus far:
public function getPaged($page, $count , $orderPersons = null)
{
$qb = $this->orderRepository->createQueryBuilder('c')
->orderBy('c.id', 'DESC');
if ($orderPersons != null )
{
foreach ($orderPersons AS $orderPerson)
{
$qb->where('c.orderPerson='.$orderPerson); ***
}
}
$query = $qb->getQuery();
}
Where I am struggling is how to write the line:
$qb->where('c.orderPerson='.$orderPerson);
I had a read of the documents and I think I need to use something like this but am not sure:
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->eq('c.orderPerson='.$orderPerson)
)
);
I am however unsure how to put this into a loop.
The one big criticism I have about the D2 documentation is that they spend a lot of time on exprs but most of the time you really don't need them. Just makes the code hard to read.
Having said that, you don't need OR conditions for your query, just an IN clause.
// Join the order persons
$qb->leftJoin(c.orderPersons,'orderPerson');
// Need at least one
if (is_array($orderPersons) && count($orderPersons)) {
$qb->andWhere('orderPerson.id IN (:orderPersons));
$qb->setParameter('orderPersons',$orderPersons);
}
Untested of course so there may be a syntax error but you should get the idea.
I am trying to understand why is it taking so long to execute a simple query.
In my local machine it takes 10 seconds but in production it takes 1 min.
(I imported the database from production into my local database)
select *
from JobHistory
where dbo.LikeInList(InstanceID, 'E218553D-AAD1-47A8-931C-87B52E98A494') = 1
The table DataHistory is not indexed and it has 217,302 rows
public partial class UserDefinedFunctions
{
[SqlFunction]
public static bool LikeInList([SqlFacet(MaxSize = -1)]SqlString value, [SqlFacet(MaxSize = -1)]SqlString list)
{
foreach (string val in list.Value.Split(new char[] { ',' }, StringSplitOptions.None))
{
Regex re = new Regex("^.*" + val.Trim() + ".*$", RegexOptions.IgnoreCase);
if (re.IsMatch(value.Value))
{
return(true);
}
}
return (false);
}
};
And the issue is that if a table has 217k rows then I will be calling that function 217,000 times! not sure how I can rewrite this thing.
Thank you
There are several issues with this code:
Missing (IsDeterministic = true, IsPrecise = true) in [SqlFunction] attribute. Doing this (mainly just the IsDeterministic = true part) will allow the SQLCLR UDF to participate in parallel execution plans. Without setting IsDeterministic = true, this function will prevent parallel plans, just like T-SQL UDFs do.
Return type is bool instead of SqlBoolean
RegEx call is inefficient: using an instance method once is expensive. Switch to using the static Regex.IsMatch instead
RegEx pattern is very inefficient: wrapping the search string in "^.*" and ".*$" will require the RegEx engine to parse and retain in memory as the "match", the entire contents of the value input parameter, for every single iteration of the foreach. Yet the behavior of Regular Expressions is such that simply using val.Trim() as the entire pattern would yield the exact same result.
(optional) If neither input parameter will ever be over 4000 characters, then specify a MaxSize of 4000 instead of -1 since NVARCHAR(4000) is much faster than NVARCHAR(MAX) for passing data into, and out of, SQLCLR objects.