参考文档:https://www.cnblogs.com/orzlin/p/10262393.html
简单单字段查询:
【正确语句1-0】: { "query":{ "match":{ "_id":"100000544" } } } 错误示例:只能单字段,如果包含2个match就会覆盖(等同于上面的【正确语句1-0】,因为会覆盖的) { "query":{ "match":{ "spuId":"136" }, "match":{ "_id":"100000544" } } }
这时候就想到了多条件并且关系怎么表示呢?
【正确语句2-0】: 相当于获取_id=100000607并且spuId=136的记录 { "query": { "bool": { "must": [ { "match": { "_id": "100000607" } }, { "match": { "spuId": "136" } } ] } } } 那么为了更好的去操作这类事情,我们可以把【正确语句1-0】改写成 【正确语句1-1】: { "query": { "bool": { "must": [ { "match": { "_id": "100000544" } } ] } } }
那么and是搞定了,告诉我OR该怎么办?
【正确语句2-1】: { "query": { "bool": { "should": [ { "match": { "_id": "100000607" } }, { "match": { "spuId": "136" } } ] } } }
这时候我们发现【正确语句2-1】好像不仅仅是or还是in的感觉,那么我需要not in的文档
【正确语句2-2】: { "query": { "bool": { "must_not": [ { "match": { "_id": "100000544" } }, { "match": { "spuId": "136" } } ] } } }
这时候如果一个字段有多个值怎么办
【正确语句2-3】: { "query": { "bool": { "should": [ { "terms": { "_id": [ "100000544", "100000548" ] } } ] } } }
范围查询
【正确语句2-4】 { "query": { "bool": { "must": { "range": { "spuId": { "gte": "130", "lte": "140" } } } } } }
中文全词,like右匹配的表达式
【正确语句3-1】 { "query": { "bool": { "must": { "match_phrase": { "spuName": "雷朋光学框" } } } } } match_phrase和match的区别在于 match能搜索出雷朋X光学镜,match_phrase只能搜索出以“雷朋光学框”为整体的数据
关于更新,我们现在只是把es作为搜索方式,所以一般都是全字段更新
PUT http://xxx.xxx/products_index_baodao/product/100000757 { "goodsId":"100000543", "title": "My first blog entry", "text": "I am starting to get the hang of this...", "date": "2014/01/02" } 代表把products_index_baodao库的,product表的,_id是100000757的记录更新成如上的格式 当然新增也是一样的,所以是不是很简单呢?如果_id不存在会自动添加当前记录,如下_id=100000757是更新进去的,_id=100000543是新增 进去的
批量根据条件更新
test/_update_by_query { "script": { "inline": "ctx._source.age=35" }, "query": { "bool": { "should": [ { "terms": { "_id": [ "2", "1" ] } } ] } } } 需要修改sudo vim /etc/elasticsearch/elasticsearch.yml script.groovy.sandbox.enabled: true #3.2的,5.5没了 务必注意配置不要顶格写,要空一格,否则 ES 无法启动 script.inline: on #务必注意配置不要顶格写,要空一格,否则 ES 无法启动 script.indexed: on #3.2的,5.5没了 务必注意配置不要顶格写,要空一格,否则 ES 无法启动 script.engine.groovy.inline.update: on #务必注意配置不要顶格写,要空一格,否则 ES 无法启动 /etc/init.d/elasticsearch restart 重启
参考文档:https://blog.csdn.net/luanpeng825485697/article/details/83411704
演示需要用到的
demo1:test/_search { "query": { "bool": { "must": { "match": { "task_name": "张" } } } }, "size": 10 } demo2:test/_analyze { "text": "张李胖子" } demo3:test/_analyze { "analyzer": "ik_max_word", "text": "张李胖子" } demo4:test/_bulk【注意不是标准json,要用postman的来执行】 {"create":{"_index":"test","_type":"man","_id":7}} {"age":18,"name":"张三丰"} {"create":{"_index":"test","_type":"man","_id":8}} {"age":17,"name":"张三丰2"} {"create":{"_index":"test","_type":"man","_id":9}} {"age":16,"name":"张三丰3"} 其中create相当于insertAll的功能,如果使用saveAll的功能,需要使用index替换create变成 {"index":{"_index":"test","_type":"man","_id":7}} {"age":18,"name":"张三丰"} {"index":{"_index":"test","_type":"man","_id":8}} {"age":17,"name":"张三丰2"} {"index":{"_index":"test","_type":"man","_id":9}} {"age":16,"name":"张三丰3"} demo5:显示使用分词 { "query": { "match": { "name": { "query": "张三丰", "analyzer": "ik_max_word" } } } } 参考文档:https://elasticsearch.cn/article/771
1、_delete_by_query
{ "query": { "match": { "_id": "_analyze" } } }
2、_update_by_query
5、keyword【精确化匹配】
3、IK分词 下载连接:https://github.com/medcl/elasticsearch-analysis-ik/releases
4、自定义词库
5、热更新
1、示例文档分析
public function index() { $option = [ 'db' => 'test', //数据库名 'table' => '',//表名 'where' => [ '_AND_' => [ 'job' => 'taxi', '_OR_' => [ 'age' => 28, '_AND_' => [ 'age' => 33, 'name' => ['like','tom%'], ], ], // 'age' =>['in',[23,33]] // 'id' => ['gt' ,1], // 'date' =>[['gte','2001-01-02'],['lte','2019-06-06']],//range between // 'name' => ['like','t%'] ] ], 'order' => [ 'age' => 'desc' ], 'limit' => 5, 'page' => [ 1,3 ], 'update'=>[ 'age' => 33, 'love' => '唱 跳 rap' ], 'saveData' => [ [ '_id' => 1,//必须 插入es中的_id字段 'age' => 18, 'name' => 'tom' ], [ '_id' => 3,//必须 插入es中的_id字段 'age' => 18, 'name' => 'tom' ] ] ]; $sql1 = "(job = 'taxi' ) and (age = 28 or (age = 33 and name= 'tom')))"; $sql2 = "(name = 'aaa' and age = 17) or ( )"; //dump($rs); //dump(json_encode($rs)); }
2、debug调试
3、客群维护实战分析
1、建立索引【ps:因geo_point不支持类似于文本的keyword方式自动加载索引,所以必须先put建立索引,之后插入数据】
{ "mappings": { "store": { #type "properties": { "lnglat": { #Field "type": "geo_point" } } } } }
2、通过es项目【JOOJ内部项目】插入数据【用json形式查看数据】
{ "db": "jooj_store_test", "table": "store", "where": [], "order": [], "limit": [], "saveData": { "_id": 1, "store_name":"龙湖天街店", "lnglat":{ "lat":32.44, "lon":122.333 }, "after":"1111" } }
3、查询数据排序
{ "sort": [ { "_geo_distance": { "lnglat": { "lat": 32, "lon": 121.345 }, "order": "asc", "unit": "km", "distance_type": "plane" } } ] }
http.cors.enabled: true http.cors.allow-origin: "*"