Queries in Solr

Avatar de Vinicius

In today’s post I am going to share about queries in Solr and how they can become helpful, specially during troubleshooting sessions.

For those who doesn’t know, Sitecore uses a search engine for two purposes in a standard installation:

Content searchthis is the default search mechanism in Sitecore Experience Manager. The search engine indexes and finds content from the mastercore or web databases.
xConnect searchthe Sitecore Experience Platform uses this search. The search engine indexes and finds contacts and interactions in the Experience Database.
https://doc.sitecore.com/developers/90/platform-administration-and-architecture/en/using-solr,-lucene,-or-azure-search.html

As a side note, Solr is used in other solutions as well, and, despite the fact this post is focused and using Sitecore values, please feel free to have a look at some queries in Solr.

A couple weeks ago I was troubleshooting an index issue in Sitecore, and one of the troubleshoot steps involved to perform queries directly in Solr.

After struggling a bit, I was able to execute the queries, so please find some examples in how to perform some queries in Solr

The begin

First you have to access Solr on its web interface, and click at Collection Selector then look for the index you want to query, in my case I want sitecore_master_index

solr web interface collection selector blog vinicius deschamps

Once you selected the index you want, there are some options available, choose Query

solr web interface collection selector sitecore master index query blog vinicius deschamps

Query options

For the purpose of this post, the only field that is going to be modified to perform our queries is the q

solr web interface query blog vinicius deschamps
ParameterDescription
Request HandlerThe way you are going to interact with solrconfig.xml, and the idea is to search, so let’s leave /select
qIt is the query itself, where the sintax is built to show results
fqApplies a filter query to the search results
sortSorts the response to a query in either ascending or descending order
startSpecifies an offset (by default, 0) into the resposes at which Solr should begin displaying content
rowsControls how many rows of responses are displayed at a time (defaul value: 10)
flLimits the information included in a query
wtSpecifies the Response Writer to be used to format the query response
indent offClick this button to request that the Response Writer use indentation to make the responses more readable.
debugQueryClick this button to augment the query response with debugging information, including “explain info” for each document returned. This debugging information is intended to be intelligible to the administrator or programmer.
dismaxClick this button to enable the Dismax query parser
edismaxClick this button to enable the Extended query parser
hlClick this button to enable highlighting in the query response
facetEnables faceting, the arrangement of search results into categories based on indexed terms.
spatialClick to enable using location data for use in spatial or geospatial searches.
spellcheckClick this button to enable the Spellchecker, which provides inline query suggestions based on other, similar, terms
https://lucene.apache.org/solr/guide/7_2/query-screen.html

First query

Let’s use the initial parameter *:* and press Execute Query to see some results

solr query first query response blog vinicius deschamps

Query Syntax

Fields

Everything after the “response” output in bold are fields that can be used to compose our query

Values

Everything after “field”: either in green or red are the values that can be used to create our query

Query examples

The fields values are reflecting a Sitecore index but the concept behind the queries is the same, since it is executed in Solr.

Keyword match

solr query syntax parsedlanguage s blog vinicius deschamps
solr query syntax parsedlangugage output blog vinicius deschamps

If you observe the first arrow pointing to “numFound”, there are 49176 items found with the following query, and the parsedlanguage_s as expected showing english

Wildcard match

solr query syntax wildcard uniqueid blog vinicius deschamps
solr query syntax uniqueid output blog vinicius deschamps

Using the query _uniqueid:*lang=en* means I want to find lang=en anywhere in the uniqueid field, and as you can see lang=en is in a random position of the value.

Multiple fields

solr query sintax uniqueid parenttemplate s smallcreateddate tdt multiple fields blog vinicius deschamps
solr query syntax uniqueid parenttemplate s smallcreateddate tdt output multiple fields blog vinicius deschamps

The syntax has slighty changed to accept the AND in order to search for multiple fields but you are able to play around and reduce the results using additional fields

Guid search

In Sitecore the GUIDs are provided like this {207939C05-FCD8-4BCC-918E-09CE0440F4C1}, and in order to search them in Solr you have to change it as follows

solr query syntax uniqueid guid syntax blog vinicius deschamps
solr query syntax uniqueid guid syntax output blog vinicius deschamps

As you can see the GUID search needs to happen in lower case and without the brackets, otherwise it won’t work

I hope you liked it, and I’ll see you on my next post

Photo by N. on Unsplash

Tagged in :

Avatar de Vinicius

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *