如何在kademi中按索引类型过滤搜索结果?

时间:2015-10-16 13:02:30

标签: api search profile kademi

以下是我使用搜索应用程序中的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 = profile获取index name = profile的数据。

enter image description here

1 个答案:

答案 0 :(得分:0)

你应该做出一些改变 首先,不应直接命名索引(例如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>