Graffiti CMS:搜索自定义字段

发布于 2024-08-18 04:29:26 字数 162 浏览 4 评论 0原文

我正在尝试找出一种方法来搜索帖子的自定义字段。基本上,我需要的是找到一篇帖子,其中 post.CustomField1 == "some value"

我已经搜索并搜索并挖掘了 Graffiti CMS 源代码 (graffiticms.codeplex.com),但无法弄清楚我如何会这样做。

I am trying to figure out a way to search through posts' custom fields. Basically, what I need is to find a post where post.CustomField1 == "some value"

I've searched and searched and been digging through the Graffiti CMS source code (graffiticms.codeplex.com) and can't figure out how I would do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-08-25 04:29:26

正如 EJB 所说,解决方案因您要实现搜索的位置而异。

如果您想查找具有特定自定义字段值的帖子,只需搜索当前页面上显示的帖子(例如index.view或类别视图),您可以在模板中使用Chalk来完成,如下所示:

#foreach($post in $posts)
  #if($post.Custom("CustomField1") == "some value")
    display or do something with $post
  #end
#end

您也可以使用 API 迭代所有帖子并检查自定义值。不幸的是,Graffiti CMS 没有内置方法来根据特定的自定义字段值查询数据库中的帖子。

但是您可以使用内置的基于 Lucene 的搜索引擎。如果您想使用搜索来搜索特定的自定义字段值,则需要对 Graffiti.Core.SearchIndex 类中的源代码进行一些调整。在 CreateDocument 方法中,将自定义字段值添加到索引文档,如下所示:

doc.Add(Field.Text("CustomField1", t.Custom("CustomField1") ?? string.Empty));

然后在 GetQueryParser 方法中,将该键添加到要搜索的字段列表中:

return new MultiFieldQueryParser(new string[] { "body", "title", "CustomField1" }, a);

通过上述两项更改,您将能够搜索“某个值”并让它返回具有 CustomField1 值的任何帖子。

希望有帮助!

As EJB said, the solution varies with where you want to implement the search.

If you want to find a post with a particular custom field value, searching on just the posts displayed on the current page (such as index.view or a category view) you could do it with Chalk in a template like this:

#foreach($post in $posts)
  #if($post.Custom("CustomField1") == "some value")
    display or do something with $post
  #end
#end

You could also use the API to iterate through ALL posts and check for the custom value. Unfortunately Graffiti CMS doesn't have a built-in method to query the database for posts based on a specific custom field value.

However you could use the built-in Lucene-based search engine. If you want to enable searching for a particular custom field value using search, you'd need to make a couple tweaks to the source code in the Graffiti.Core.SearchIndex class. In the CreateDocument method add the custom field value to the indexed Document like this:

doc.Add(Field.Text("CustomField1", t.Custom("CustomField1") ?? string.Empty));

Then in the GetQueryParser method add that key to the list of fields to search on:

return new MultiFieldQueryParser(new string[] { "body", "title", "CustomField1" }, a);

With the two changes above, you'd be able to do a search for "some value" and have it return any posts with CustomField1 value of that.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文