How do I setup a Lunr search? #16
-
This follows on from #15, my wyam based blog has a search based on the SearchIndex module and these custom pipelines configured in the wyam.config // https://randalvance.net/post/implementing-searching-wyam.html
// https://github.com/cvan/lunr-unicode-normalizer
Pipelines.Add("PostsSearch",
ReadFiles("posts/*.md"),
FrontMatter(Yaml()),
Markdown(),
Meta("SearchIndexItem", new SearchIndexItem("/" + @doc.String("RelativeFilePathBase"), @doc.String("Title"), @doc.String("Tags"))
{ Description = @doc.String("Lead"), Tags = @doc.String("Tags") })
);
Pipelines.Add("SearchIndex",
Documents("PostsSearch"),
SearchIndex((FilePath)"stopwords.txt"),
WriteFiles((doc,ctx) => string.IsNullOrEmpty(doc.Content) ? null : "assets/js/searchindex.js").UseWriteMetadata(false)
); I found the [discussion on renaming modules which explains Something like this gets me close (also tried await Bootstrapper
.Factory
.CreateWeb(args)
.ConfigureEngine(x => x
.Pipelines[nameof(Statiq.Web.Pipelines.Content)]
.ProcessModules
.Add(new GenerateLunrIndex(stopwordsPath: "/stopwords.txt").WithDestination("/assets/js/searchindex.js"))) In But after that I get an empty array for if (searchIndexItems.Length == 0)
{
context.LogWarning("It's not possible to build the search index because no documents contain the necessary metadata.");
return Array.Empty<IDocument>();
} What is the best way to hookup the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Merry Christmas, a good time for me to look at some issues, so i came up with a pipeline that is working for me to get the same output as my wyam blog, still some config issues to tidy up, but it works. public class SearchIndex : Pipeline
{
public SearchIndex()
{
Dependencies.AddRange(
nameof(Inputs),
nameof(Content),
nameof(Archives),
nameof(Data));
var docs = new GetPipelineDocuments(ContentType.Content);
ProcessModules = new ModuleList
{
//new ExecuteIf(Config.FromSetting(CustomKeys.GenerateSearchIndex, false))
new ExecuteIf(IExecutionContext.Current.GetBool("GenerateSearchIndex"))
{
new GetPipelineDocuments(ContentType.Content),
// Filter to non-archive content
new FilterDocuments(Config.FromDocument(doc => !Archives.IsArchive(doc))),
//new FilterDocuments(Config.FromDocument(doc => doc.GetBool("IsPost"))),
new ForEachDocument
{
new ExecuteConfig(Config.FromDocument((searchDoc, ctx) =>
{
ModuleList modules = new ModuleList();
// this isn't right, would like to use IsPost but it doesn't seem to be available
if(searchDoc.Destination.Parent.Name.Equals("posts", StringComparison.InvariantCultureIgnoreCase)){
var searchItem = new LunrIndexDocItem(
searchDoc,
searchDoc.GetTitle(),
searchDoc.GetContentStringAsync().Result);
searchItem.Description = searchDoc.GetString("Lead");
searchItem.Tags = searchDoc.GetString("Tags");
modules.Add(new SetMetadata("LunrIndexItem",
searchItem
));
}
return modules;
}))
},
new GenerateLunrIndex("LunrIndexItem", "stopwords.txt", true)
.WithDestination("assets/js/SearchIndex.js")
}
};
OutputModules = new ModuleList
{
new WriteFiles()
};
}
} |
Beta Was this translation helpful? Give feedback.
Merry Christmas, a good time for me to look at some issues, so i came up with a pipeline that is working for me to get the same output as my wyam blog, still some config issues to tidy up, but it works.