ElastiSearch使用笔记

在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定义的名称对应
});


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值