ElastiSearch使用笔记

来源:互联网 发布:mac版pscc下载 编辑:程序博客网 时间:2024/05/20 11:24

在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:truefunction (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 operationlogesClient.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
原创粉丝点击