Create a JSONPath document extractor
Create an XPath document extractor when you want to use an XPath expression to extract attribute values in a Sitecore Search source.
To create a JSONPath document extractor matching URLs with a JavaScript function:
-
On the menu bar, click Sources.
-
Select the source you created.
-
On the Source Settings page, next to Document Extractors, click Edit
.
-
To create a document extractor, on the Document Extractors page:
-
In the Name field, enter a meaningful name for the extractor.
For example, enter Sitecore cloud.
-
In the Extractor Type drop-down menu, click JSONPath.
-
Optionally, to ensure that this extractor's logic only applies to URLs that match a certain pattern, configure URLs to Match. To do this, in the URLs To Match field, click
Add Matcher, select the TYPE of expression you want to use, and enter the VALUE of that expression.
For example, click JS in the TYPE drop-down menu and enter the following expression to use JavaScript to ensure that the extractor only extracts attributes from API endpoints whose response has a value for
body.data.item.rendered
.RequestResponsefunction match(request, response) { return response.body.data.item.rendered != null && response.body.data.item.rendered.sitecore.route.placeholders['headless-main'].length > 0; }
-
-
In the Taggers section, click Add Tagger . Then, in the tag editor, select a tag in the Tag drop-down menu. For example, select content.
NoteIn a document extractor, you can create multiple taggers where each is linked to a unique tag. This way each tagger:
-
Generates a set of index documents.
-
Can have multiple rules such that each rule defines the extraction logic of one attribute.
For example, one tagger with five rules yields one set of documents each with five attributes.
Three taggers with one rule each yields three sets of documents each with one attribute.
-
-
In the tag editor, enter the following details to extract an attribute:
-
In the Attribute drop-down menu, click the attribute you want to configure.
For example, click Description.
-
In the Value type drop-down menu, choose whether you want the attribute value to be a fixed value or an expression.
For example, click Expressions.
-
In the EXPRESSION field, enter a JSONPath expression that results in the attribute value.
For example, to get the value of description from the of the
..placeholders['headless-main']..fields.Description
key, enterRequestResponse..placeholders['headless-main']..fields.Description.value
-
-
Optionally, to configure more than one way to get an attribute value, click
Add Selector. Then, in the Expression field of the second selector, enter the JSONPath expression that results in the attribute value.
For example, as a second option, you want to get the value of description from the
..placeholders['headless-main']..fields.Text
tag. To get this, enter the following JSONPath expression:RequestResponse..placeholders['headless-main']..fields.Text.value
NoteWhen there are multiple selectors, Search runs them in chronological order and stops when it arrives at an expression that gives a result.
-
To configure how to extract other attributes, click Add Rule
and repeat steps 5 and 6.
For example, now that you've configured how to extract the description attribute, you can configure how to extract the title, subtitle, and image attributes.
-
In the tag editor, click Save.
-
(Optional) To extract attributes for another tag, click
Add Tagger, and in the Tag drop-down menu, click a tag and repeat Steps 5 through 8.
-
On the Document Extractors page, click Save.