在JS端使用elastic.js 处理
1.查询:
body = ejs.Request().query(ejs.QueryStringQuery("检索字符串").defaultOperator('AND')).highlight(ejs.Highlight('*'));//关键词检索
ejs.RegexpQuery(param.name,param.value) //正则表达式检索
ejs.WildcardQuery(param.name,param.value);//通配符检索
ejs.RangeQuery(param.name).from(param.from).to(param.to)//范围检索
ejs.FuzzyQuery(param.name,param.value)//模糊检索
ejs.PrefixQuery(param.name,param.value);//前缀匹配
esClient.search({
index:indices,//索引库的数组
type:types,//索引表的数组
body:body,
size:99999
},function(err,resp){
var result = resp.hits.hits;
});
2.ajax查询
字段多条件: 字段名:值 and (or) 字段名:值 and(or)
queryStr += '字段名:\"'+值+'\" OR 字段名:值';
或者是拼装json
1、多条件,默认字段的
var queryData = {
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "dictionary.dictype",
"query": "呵呵"
}
},
{
"query_string": {
"default_field": "dictionary.name",
"query": "哈哈"
}
}
}
}
};
2、使用文档ID作为条件的
queryData = "{\"query\": {\"bool\": {\"must\": [{\"query_string\": {\"default_field\": \"_id\",\"query\": \""+idTemp+"\"}}]}}}"
$.ajax({
url : "http://"+window.location.host+"/system/dictionary/_search",
method : "post",
dataType : "json",
async:false,
data : JSON.stringify(queryData),
success : function(resp) {
var hits = resp.hits.hits;
});
3.更新
var t=form.serializeForm();
esClient.update({
index: index,
type: type,
id:id,
refresh:true,
body: {doc:t}
}, function (error, response) {
});
4.插入数据
esClient.create({
index: index,
type: type,
body: t,
refresh:true
function (error, response) {
});
5.删除
esClient.delete({
index: index,
type: type,
id: id,
refresh:true
}, function (error, response) {
});
6.统计
//select audit_keyword,count(*) as keyword_agg from operationlog group by operationlog
esClient.search({
searchType:'count',
index:'user_operation_log',
type:'operationlog',
body:ejs.Request().agg(ejs.TermsAggregation('keyword_agg').field('audit_keyword'))
},function(err,resp){
aggResult=resp.aggregations.keyword_agg.buckets;//跟上面的TermsAggregation定义的名称对应
});