【问题标题】:How to filter search result by index type in kademi?如何在 kademi 中按索引类型过滤搜索结果?
【发布时间】:2016-01-15 05:59:44
【问题描述】:

这是我的代码 sn-p,用于使用搜索应用程序中的 SearchManager API 从 kademi 网站搜索个人资料、博客和内容。

keyword = params['q'];

var json = {
        "query": { 
            "match": {"_all":keyword}
        },

        "highlight": {
            "fields" : {
                "*" : {},
                "content" : {
                    "type" : "plain"
                }
            }
        }
    };

var indexes = ["profile", "bran-103166797", "blogs-103166797"]; // profile, content, blog
var sm = applications.search.searchManager;
var result = sm.search(JSON.stringify(json), indexes);

如果您在下面看到我的屏幕截图,则索引名称 = 配置文件有几种索引类型。我只想从index type = profileindex name = profile 获取数据。

【问题讨论】:

    标签: api search profile kademi


    【解决方案1】:

    您应该进行一些更改 首先,不要直接命名索引(例如 bran-103166797),您应该使用 AppIndexers 以便生成正确的名称。否则,当您发布网站的新版本时,您的搜索仍将索引旧版本:

            var sm = applications.search.searchManager;
            var indexers = sm.appIndexers;
            var profileIndexer = indexers.profile;
            var contentIndexer = indexers.content;
    

    然后您可以使用 SearchManager 上的 prepareSearch 方法,它可以让您直接操作搜索构建器:

            log.info("using indexers {} {}", profileIndexer, contentIndexer);
            var builder = sm.prepareSearch(profileIndexer, contentIndexer);
            builder.setSource(JSON.stringify(json));
            builder.setTypes("profile", "html");
    

    然后您可以使用 elasticsearch API 方法执行搜索查询。请注意,在此示例中,我使用的是内联 js 脚本而不是 js 控制器,因此我需要在请求属性中设置结果,以便模板可以访问它。

            var result = builder.execute().actionGet();
            log.info("result {}", result);
            http.request.attributes.result = result; 
    

    这是一个完整的示例: http://docs.kademi.co/howtos/devs/advanced-search-pages-with-the-searchmanager-api.html

    该示例中的模板来源在这里:

    <html>
    <head>
        <title>search page</title>
    </head>
    <body>
        #script()
        <script>
            var keyword = http.request.params.q;
    
            var json = {
                    "query": { 
                        "match": {"_all":keyword}
                    },
                    "fields" : ["nickName", "title"],
                    "highlight": {
                        "fields" : {
                            "*" : {},
                            "content" : {
                                "type" : "plain"
                            }
                        }
                    }
                };
    
            var sm = applications.search.searchManager;
            var indexers = sm.appIndexers;
            var profileIndexer = indexers.profile;
            var contentIndexer = indexers.content;
            log.info("using indexers {} {}", profileIndexer, contentIndexer);
            var builder = sm.prepareSearch(profileIndexer, contentIndexer);
            builder.setSource(JSON.stringify(json));
            builder.setTypes("profile", "html");
            var result = builder.execute().actionGet();
            log.info("result {}", result);
            http.request.attributes.result = result; // make available to templating
        </script>
        #end
    
        <div class="container">
            <h1>Search</h1>            
            <p class="pull-right lead">Showing $request.attributes.result.hits.hits.size() of $request.attributes.result.hits.totalHits hits</p>
            <table class="table table-striped">
            #foreach( $hit in  $request.attributes.result.hits)
            <tr>
                <td>
                    $!hit.fields.nickName.value $!hit.fields.title.value
                </td>
                <td>$hit.type</td>
            </tr>
            #end
            </table>
        </div>
    
        <!-- for debugging, just display the search result as json -->
        <pre>$request.attributes.result</pre>                
    </body>
    

    【讨论】:

    • 我没有明确说,但是上面的方法允许设置文档类型来匹配
    猜你喜欢
    • 2020-04-07
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多