ArangoDB Dynamic AQL String Handler - string

Is it possible to use the aql string handler in ArangoDB to perform a dynamic query? I've tried a number of different ways but it always errors out. For example, I'd like to do something like this:
let sortExpression = sortByDate ? 'SORT ${date}' : `SORT ${name}`
const result = db._query(aql`
FOR doc IN tickets
${sortExpression}
RETURN doc
`)
This example is very simplified, but I have a more complex situation where something like this would be really handy. Is there a way to make something like this work?

If the query was more complex, and I had real variables to embed, I'd write this like the following:
let sortProp = sortByDate ? 'date' : 'name';
var query = aql`
FOR doc IN tickets
SORT #SORT_PROP#
RETURN doc`;
query.query = query.query.replace('#SORT_PROP#', sortProp);
var result = db._query(query);
Maybe this works for your use case, too.

After looking more closely at how the aql string handler works, it turns out what I'm trying to do is just not feasible with it. So I decided to use the regular bind var syntax:
let sortExpression = 'SORT #sortField'
const result = db._query(`
FOR doc IN ##coll
${sortExpression}
RETURN doc
`, {
'#coll': module.context.collectionName('tickets'),
'sortField': sortByDate ? 'date' : 'name'
})
Again this is overkill for the simple example above, but my real world query is much more complex.

Related

Kentico 10 ObjectQuery join multiple tables

I am basically trying to run a query that gives me all the Users that have purchased a product with a particular SKU. Essentially this SQL here:
SELECT u.FirstName, u.LastName, u.Email
FROM COM_OrderItem oi INNER JOIN COM_Order o ON oi.OrderItemOrderID = o.OrderID
INNER JOIN COM_Customer c ON o.OrderCustomerID = c.CustomerID
INNER JOIN CMS_User u ON c.CustomerUserID = u.UserID
WHERE oi.OrderItemSKUID = 1013
I was trying to use the ObjectQuery API to try and achieve this but have no idea how to do this. The documentation here does not cover the specific type of scenario I am looking for. I came up with this just to try and see if it works but I don't get the three columns I am after in the result:
var test = OrderItemInfoProvider
.GetOrderItems()
.Source(orderItems => orderItems.Join<OrderInfo>("OrderItemOrderID", "OrderID"))
.Source(orders => orders.Join<CustomerInfo>("OrderCustomerID", "CustomerID"))
.Source(customers => customers.Join<UserInfo>("CustomerUserID", "UserID"))
.WhereEquals("OrderItemSKUID", 1013).Columns("FirstName", "LastName", "Email").Result;
I know this is definitely wrong and I would like to know the right way to achieve this. Perhaps using ObjectQuery is not the right approach here or maybe I can somehow just use raw SQL. I simply don't know enough about Kentico to understand the best approach here.
Actually, the ObjectQuery you created is correct. I tested it and it is providing the correct results. Are you sure that there are indeed orders in the system, which contain a product with SKUID 1013 (you can check that in the COM_OrderItem database table)?
Also, how are you accessing the results? Iterating through the results should look like this:
foreach (DataRow row in test.Tables[0].Rows)
{
string firstName = ValidationHelper.GetString(row["FirstName"], "");
string lastName = ValidationHelper.GetString(row["LastName"], "");
string email = ValidationHelper.GetString(row["Email"], "");
}

Function for `fq` field of SOLR in SOLR-node-client

For various fields such as q , start , row etc in SOLR we have corresponding functions in SOLR-node-client.
So if I want to construct a query for the following:
http://host:port/solr/eposro/select?q=cats.0%3A1&start=0&rows=4&wt=json&indent=true
I can use something like this:
var query = client.createQuery()
.q({cats.0 : 1})
.start(0)
.rows(4);
However, there is a filter query field in SOLR, fq. I don't seem to find a corresponding function for this in SOLR-node-client.
Following gives me error:
var query = client.createQuery()
.q({cats.0 : 1})
.fq({'brand':'real'})
.start(0)
.rows(4);
I get an error saying that fq function doesn't exist.
Am I doing anything wrong or is there any other way to achieve filter query using SOLR-node-client?
createQuery() returns a Query object and it has a matchFilter method.
Example:
var query = client.createQuery()
.q({cats.0 : 1})
.matchFilter('brand', 'real')
.start(0)
.rows(4);
HTH
I have checked a bit the source code and found out:
So, the usage should be like,
let searchQuery = solrClient.query()
searchQuery = searchQuery.fq({field:"tags",value: this.filterTag});
If anyone can update the doc, that would be great.

Search in new Sitecore ContentSearch using whole words

I am using new Sitecore search, and the issue I ran into is having results come for words that have nothing to do with my search term.
For example, searching for "lies" will find "applies". And this is not what I am looking for.
This is an example of search I am doing (simplified). It is a direct LINQ check for "Contains" on the "Content" property of the SearchResultItem, and most likely not what I supposed to do. It is just happen to be that samples I find online are practically doing so.
Example of my code (simplified). In here I break down the search sentence to separate keywords. By the way, I am looking for a way to show full sentence match first.
using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
{
var results = context.GetQueryable<SearchResultItem>()
.Filter(i => i.Path.StartsWith(Home.Paths.FullPath))
.Filter(GetTermPredicate(Term));
// use results here
}
protected Expression<Func<SearchResultItem, bool>> GetTermPredicate(string term)
{
var predicate = PredicateBuilder.True<SearchResultItem>();
foreach (var tempTerm in term.Split(' '))
{
predicate = predicate.And(p => p.Content.Contains(tempTerm));
}
return predicate;
}
Thank you in advance!!
All right. I got help from Sitecore Support.
In my version of Sitecore I can use the following to acheive search for a whole word instead of partial:
instead of:
predicate = predicate.And(p => p.Content.Contains(tempTerm));
use
predicate = predicate.And(p => p.Content.Equals(tempTerm));
Issue solved.
Replace Filter in your code by Where, it should be fine,
here is an example :
var currentIndex = ContentSearchManager.GetIndex("sitecore_web_index");
using (var context = currentIndex.CreateSearchContext())
{
var predicate = PredicateBuilder.True();
foreach (var currentWord in term.Split(‘ ‘))
{
predicate = predicate.Or(x => x.Content.Contains(currentWord ));
}
var results = context.GetQueryable().Where(predicate).GetResults();
}
As Ahmed notes, you should use Where instead of Filter, since Filter has no effect on search rank. The classic use case for filters is to apply a facet chosen by the user without distorting the ordering of results, as would happen if you used a Where clause.
Filtering is similar to using Where to restrict the result list. Both methods will affect the result in the same
result list, but when you use a Filter the scoring/ranking of the search hits is not affected by the filters.
Developer's Guide to Item Buckets and Search
There's a good dicussion of when to use Filter on the Sitecore 7 team blog:
Making Good Part 4: Filters, Paging and I'm feeling lucky.
If you only want to search for whole words, you could prefix and postfix the searchterm with a space. Allthough this doesn't catch all situations.

What is wrong in this LINQ Query, getting compile error

I have a list AllIDs:
List<IAddress> AllIDs = new List<IAddress>();
I want to do substring operation on a member field AddressId based on a character "_".
I am using below LINQ query but getting compilation error:
AllIDs= AllIDs.Where(s => s.AddressId.Length >= s.AddressId.IndexOf("_"))
.Select(s => s.AddressId.Substring(s.AddressId.IndexOf("_")))
.ToList();
Error:
Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<MyCompany.Common.Users.IAddress>'
AllIDs is a list of IAddress but you are selecting a string. The compiler is complaining it cannot convert a List<string> to a List<IAddress>. Did you mean the following instead?
var substrings = AllIDs.Where(...).Select(...).ToList();
If you want to put them back into Address objects (assuming you have an Address class in addition to your IAddress interface), you can do something like this (assuming the constructor for Address is in place):
AllIDs = AllIDs.Where(...).Select(new Address(s.AddressID.Substring(s.AddressID.IndexOf("_")))).ToList();
You should also look at using query syntax for LINQ instead of method syntax, it can clean up and improve the readability of a lot of queries like this. Your original (unmodified) query is roughly equivalent to this:
var substrings = from a in AllIDs
let id = a.AddressId
let idx = id.IndexOf("_")
where id.Length >= idx
select id.Substring(idx);
Though this is really just a style thing, and this compiles to the same thing as the original. One slight difference is that you only have to call String.IndexOf() one per entry, instead of twice per entry. let is your friend.
Maybe this?
var boundable =
from s id in AllIDs
where s.AddressId.Length >= s.AddressId.IndexOf("_")
select new { AddressId = s.AddressId.Substring(s.AddressId.IndexOf("_")) };
boundable = boundable.ToList();

Subsonic load objects by a list of ids

Is it possible to load objects by a list of ids using subsonic ActiveRecord?
My code looks like:
IList<Video> videos = Video.Find(v => videoIds.Contains(v.ID));
I get an exception: The method 'Contains' is not supported
Do I do something wrong ... or I hit one of subsonic's limitations?
Thanks, Radu
After more research I found a way to achieve this:
List<int> videoIds = new List<int>(){1, 2, 3, 4, 5};
SqlQuery query = new Select().From<Video>().Where("ID").In(videoIds);
List<Video> videos = query.ExecuteTypedList<Video>();
FYI: SubSonic's linq parser does not like generic Lists and Contains
// does not work
List<int> videoIds = new List<int>() {1,2,3,4,5};
var videos = Video.Find(v => videoIds.Contains(v.ID));
// should work
IEnumerable<int> videoIds = new List<int>() {1,2,3,4,5};
var videos = Video.Find(v => videoIds.Contains(v.ID));
Noticed the difference?
Sounds strange, but whenever you want to use Contains() with Subsonic, you first have to cast your List to an IEnumerable to prevent the NotSupportedException.
This is the one-liner I believe you where looking for.
var colMatchingVideos = Video.Find( objVideo => colVideoIds.Any( iVideoId => objVideo.ID == iVideoId).ToList();
Also I would strongly advise you avoid using string literals for columns, instead you could use Video.Columns.Id or expressions Where( o => o.Id). This would ensure that if you change your column names in the Database a compile time exception will occur. Help's a lot with maintainability.

Resources