Api 如何在kademi中按索引类型筛选搜索结果?
下面是我的代码片段,使用搜索应用程序中的SearchManager API从kademi站点搜索个人资料、博客和内容Api 如何在kademi中按索引类型筛选搜索结果?,api,search,profile,kademi,Api,Search,Profile,Kademi,下面是我的代码片段,使用搜索应用程序中的SearchManager API从kademi站点搜索个人资料、博客和内容 keyword = params['q']; var json = { "query": { "match": {"_all":keyword} }, "highlight": { "fields" : { "*" : {},
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 name=profile有几种索引类型。我只想从索引类型=profile
和索引名称=profile
中获取数据
您应该做一些更改 首先,您不应该直接命名索引(例如bran-103166797),而应该使用AppIndexer来生成正确的名称。否则,当您发布网站的新版本时,您的搜索仍将索引旧版本:
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");
然后可以使用ElasticSearchAPI方法执行搜索查询。请注意,在本例中,我使用的是内联js脚本,而不是js控制器,因此我需要在请求属性中设置结果,以便模板可以访问它
var result = builder.execute().actionGet();
log.info("result {}", result);
http.request.attributes.result = result;
下面是一个完整的工作示例:
该示例中模板的来源如下:
<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>
搜索页面
#脚本()
var关键字=http.request.params.q;
var json={
“查询”:{
“匹配”:{“_all”:关键字}
},
“字段”:[“昵称”、“标题”],
“亮点”:{
“字段”:{
"*" : {},
“内容”:{
“类型”:“普通”
}
}
}
};
var sm=applications.search.searchManager;
var索引器=sm.AppIndexer;
var profileIndexer=indexers.profile;
var contentIndexer=indexers.content;
info(“使用索引器{}{}”、profileIndexer、contentIndexer);
var builder=sm.prepareSearch(profileIndexer、contentIndexer);
builder.setSource(JSON.stringify(JSON));
setTypes(“profile”、“html”);
var result=builder.execute().actionGet();
log.info(“结果{}”,result);
http.request.attributes.result=result;//使模板可用
#结束
搜寻
显示$request.attributes.result.hits.hits.hits.size()的$request.attributes.result.totalHits
#foreach($request.attributes.result.hits中的hit)
$!hit.fields.昵称.value$!hit.fields.title.value
$hit.type
#结束
$request.attributes.result
您应该做一些更改 首先,您不应该直接命名索引(例如bran-103166797),而应该使用AppIndexer来生成正确的名称。否则,当您发布网站的新版本时,您的搜索仍将索引旧版本:
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");
然后可以使用ElasticSearchAPI方法执行搜索查询。请注意,在本例中,我使用的是内联js脚本,而不是js控制器,因此我需要在请求属性中设置结果,以便模板可以访问它
var result = builder.execute().actionGet();
log.info("result {}", result);
http.request.attributes.result = result;
下面是一个完整的工作示例:
该示例中模板的来源如下:
<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>
搜索页面
#脚本()
var关键字=http.request.params.q;
var json={
“查询”:{
“匹配”:{“_all”:关键字}
},
“字段”:[“昵称”、“标题”],
“亮点”:{
“字段”:{
"*" : {},
“内容”:{
“类型”:“普通”
}
}
}
};
var sm=applications.search.searchManager;
var索引器=sm.AppIndexer;
var profileIndexer=indexers.profile;
var contentIndexer=indexers.content;
info(“使用索引器{}{}”、profileIndexer、contentIndexer);
var builder=sm.prepareSearch(profileIndexer、contentIndexer);
builder.setSource(JSON.stringify(JSON));
setTypes(“profile”、“html”);
var result=builder.execute().actionGet();
log.info(“结果{}”,result);
http.request.attributes.result=result;//使模板可用
#结束
搜寻
显示$request.attributes.result.hits.hits.hits.size()的$request.attributes.result.totalHits
#foreach($request.attributes.result.hits中的hit)
$!hit.fields.昵称.value$!hit.fields.title.value
$hit.type
#结束
$request.attributes.result
我没有明确地说,但是上面的方法允许将文档类型设置为匹配。我没有明确地说,但是上面的方法允许将文档类型设置为匹配