Picture of Fabian Holtermann
Software Engineer, Sitecore Certified Trainer & Expert

Today I Learned...


... that the Sitecore „FieldQuery“ is actually a prefix query.

The next couple of days I want to give you some insights into my daily working life. Have you ever had a situation, where you learned something really surprising and new while you were coding? This is what I want to share with you. I hope that it will help some of you as much as it helped me. It was quite a surprise that my search for an ID returned two items.

using (IndexSearchContext context = index.CreateSearchContext()) {     var queryForId = new FieldQuery("___Product Id", id);     var searchHits = context.Search(queryForId); }
I expected that I would only get one result back that exactly matched the ID Searching for the ID 4332 however, returned two items. One with the ID 4332 and one with the ID 4332685. Solving this is simple but took me some time to get it:

using (IndexSearchContext context = index.CreateSearchContext()) { var term = new Term(fieldName, String.Concat(id)); var termQuery = new TermQuery(term); var preparedQuery = new PreparedQuery(termQuery); var searchHits = context.Search(preparedQuery); }

The query will now only return exact matches of the ID and no longer everything that starts with the ID.

Es wurden noch keine Kommentare zu diesem Eintrag geschrieben.
Kommentar hinzufügen
Vor und Zuname
E-Mail bei weiteren Kommentaren
Mein Kommentar