Developer forum

Forum » CMS - Standard features » Is there a way to search content and get result paragraph id's?

Is there a way to search content and get result paragraph id's?

Peter Leleulya
Peter Leleulya
Reply

Hi guys,

Once upon a time we created a frequently asked questions module for a customer.
We had made a page template with some placeholders which each represent a category.
On this page you can place paragraph item types of the type Faq which have a question field, an answer, etc.
You could place these paragrpahs in the corresponding placeholder and sort it by drag and drop.
The paragraph template shows an accordion, so the page is a list of accordions and has a bit of javascript for interaction ...

This has worked perfectly for some time now.

Now there has been a new feature request, to be able to search in these faqs.
A search box on the top of the page and when searching the paragraphs that have a mach should stay on the page and the rest have to be removed from the DOM.

I tried the search model, set to search on this page specificly (and underlying pages), which does work, it recognizes the paragraphs. 
But this is made for page search and there doesnt seem to be a way to get something like paragraph ids to be able to use these ...

I tried creating a query on the contentindex and use the search term as parameter, but the querypublisher never gets my faq items as a result.
Probably not made for paragraphs either ...

Is there a way, even if it's a nasty hacky kind of way, to search paragraphs of a certain type (faq) on some fields (question, answer) and get their id's to manipulate the dom with javascript?


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

You can make an index using a SQLIndexBuilder: https://doc.dynamicweb.com/documentation-9/indexing/indexing-search/indexes#sideNavTitle1-2-3

Since all your faq items are of the same type, you can join the paragraph table witht the correct ItemType_* table, only select paragraphs of the right item type, and then you will get a number of documents in the lucene index that will have paragraphids available in the querypublisher. This way you can search, use suggestions, facets etc.

The alternative is to do a free text in pure js - use a dom query selector and iterate each FAQ item (i.e. a li node or however you do your FAQ items) and the use the textContent property of that node to and a do an includes("searchstring") on the value of contentstring.

Something like this:

const faqItemContainer = document.querySelectorAll("ul.faqContainer");
const faqItems = faqItemContainer.querySelectorAll("li");

faqItems.forEach(function(faqItem) {
  if(faqItem.textContent.includes("searchString")){
//Highlight
}
});
Votes for this answer: 1
 
Peter Leleulya
Peter Leleulya
Reply

Yeah ok ... In that case I went for the full front-end option.
Fixed it that way, even though it's not my preferred way.

Just wanted to make sure there wasn't a module option I didn't think off ... 

 

You must be logged in to post in the forum