2023年6月21日发(作者:)
php+elasticsearch查询1. elasticsearch单个查询(1)match_all 查询所有⽂档。在没有指定查询⽅式时,它是默认的查询。在PHP⽤法 不能直接⽤ “{}” $body = [ "match_all"=> new stdClass() ];(2) match 查询精确查询也可以匹配查询也可以,在PHP 中的⽤法 $body = [ "match"=>[ "shop_content"=>"天⽓" ] ]; $body = [ "match"=>[ "shop_id"=>"1" ] ];shop_content 采⽤的是elasticsearch 默认分词器 standard 则匹配数据规则会把“天⽓”分开来 只要出现”天“或者 “⽓”的都匹配出来。采⽤IK中⽂分词器ik_max_word 会根据中⽂语法来分词查询。shop_id则是精准匹配。(3)multi_match 多项字段筛选 $body = [ "multi_match"=>[ "query"=> "天天", "fields"=>[ "shop_name", "shop_content"] ] ];shop_name或者shop_content 匹配到”天天“(4)range 查询 = mysql 区间 查询 $body = [ "range"=>[ "shop_id"=>[ "gte"=>3, "lte"=>20, ] ] ];shop_id ⼤于等于3⼩于等于20 gt(⼤于) lt(⼩于)(5) trem 跟 trems 精确查询 $body = [ "term"=>["shop_id"=>1], ]; $body = [ "terms"=>["shop_id"=>[1,2,3]], ];(5)exists 查询 相等于mysql 的is not null ⽽is null 需要配合 must_not 组合查询(后⾯会提及) $body = [ "exists"=>["field"=>"shop_id"], ];以上的查询体只能在body⾥单使⽤⽆法复合使⽤。在实际查询中我多为组合查询 ⽐如:shop_id>3 and shop_name like “天⽓” andshop_num > 1 这种组合查询2. 组合查询使⽤关键词 bool 相等于声明是组合查询。must : ⽂档必须匹配must_not:⽂档必须不匹配should:任意语句匹配 相等于or 若匹配成功则增加score分filter:必须匹配 score分不增加elasticsearch 查询出的结果会按照默认score⾼到低排序 。sql: where shop_id > 1 and (shop_name like “%天天%” or shop_content like “%天天%” or shop_title like “%裤⼦%” ) $body = [ "bool"=>[ "must"=>[ "range"=>["shop_id"=>["gte"=>1]] ], "should"=>[ [ "match"=>["shop_name"=>"天天"] ], [ "match"=>["shop_title"=>"裤⼦"] ], [ "match"=>["shop_content"=>"天天"] ] ], "minimum_should_match"=>1, // should条件匹配⼀个⾜矣
必须放在 should后⾯
] ];sql: where class_name=“包包” and shop_id >1 and( shop_date>2020-01-01 or (shop_title=“裤⼦” and shop_price < 20))$body = [ "bool"=>[ "must"=>[ [ "range"=>[ "shop_id"=>["gte"=>1] ] ], [ "match"=>[ "class_name"=>"包包" ] ] ], "should"=>[ [ "range"=>[ "shop_date"=>["gte"=>"2020-12-27"] ] ], [ "bool"=>[ "must"=>[ [ "range"=>[ "shop_price"=>["gte"=>20] ] ], [ "match"=>[ "shop_title"=>"裤⼦" ] ] ], ] ] ], "minimum_should_match"=>1, ] ];sql :where shop_num is null$body = [ "bool"=>[ "must_not"=>[ "exists"=>["field"=>"shop_num"] ], ] ];3. 分页及排序排序:elasticsearch 默认排序 是_score降序。⾃⼰设定排序:必须放在body⾥ 语法格式:如下 public function search_file($index_name,$type_name,$body){ $params = [ 'index' => $index_name, 'type' => $type_name, 'body'=> [ 'query'=>$body, "sort"=>[ "shop_id"=>["order"=>"desc"] ] ], "from"=>1, "size"=>6, ]; $response = $this->client->search($params); return $response; } //
多个排序写法 "sort"=>[ ["shop_id"=>["order"=>"desc"]], ["shop_date"=>["order"=>"desc"]] ]分页:elasticsearch 默认每页10条 from:第⼏个⽂件开始 size:分页的⽂件数。
2023年6月21日发(作者:)
php+elasticsearch查询1. elasticsearch单个查询(1)match_all 查询所有⽂档。在没有指定查询⽅式时,它是默认的查询。在PHP⽤法 不能直接⽤ “{}” $body = [ "match_all"=> new stdClass() ];(2) match 查询精确查询也可以匹配查询也可以,在PHP 中的⽤法 $body = [ "match"=>[ "shop_content"=>"天⽓" ] ]; $body = [ "match"=>[ "shop_id"=>"1" ] ];shop_content 采⽤的是elasticsearch 默认分词器 standard 则匹配数据规则会把“天⽓”分开来 只要出现”天“或者 “⽓”的都匹配出来。采⽤IK中⽂分词器ik_max_word 会根据中⽂语法来分词查询。shop_id则是精准匹配。(3)multi_match 多项字段筛选 $body = [ "multi_match"=>[ "query"=> "天天", "fields"=>[ "shop_name", "shop_content"] ] ];shop_name或者shop_content 匹配到”天天“(4)range 查询 = mysql 区间 查询 $body = [ "range"=>[ "shop_id"=>[ "gte"=>3, "lte"=>20, ] ] ];shop_id ⼤于等于3⼩于等于20 gt(⼤于) lt(⼩于)(5) trem 跟 trems 精确查询 $body = [ "term"=>["shop_id"=>1], ]; $body = [ "terms"=>["shop_id"=>[1,2,3]], ];(5)exists 查询 相等于mysql 的is not null ⽽is null 需要配合 must_not 组合查询(后⾯会提及) $body = [ "exists"=>["field"=>"shop_id"], ];以上的查询体只能在body⾥单使⽤⽆法复合使⽤。在实际查询中我多为组合查询 ⽐如:shop_id>3 and shop_name like “天⽓” andshop_num > 1 这种组合查询2. 组合查询使⽤关键词 bool 相等于声明是组合查询。must : ⽂档必须匹配must_not:⽂档必须不匹配should:任意语句匹配 相等于or 若匹配成功则增加score分filter:必须匹配 score分不增加elasticsearch 查询出的结果会按照默认score⾼到低排序 。sql: where shop_id > 1 and (shop_name like “%天天%” or shop_content like “%天天%” or shop_title like “%裤⼦%” ) $body = [ "bool"=>[ "must"=>[ "range"=>["shop_id"=>["gte"=>1]] ], "should"=>[ [ "match"=>["shop_name"=>"天天"] ], [ "match"=>["shop_title"=>"裤⼦"] ], [ "match"=>["shop_content"=>"天天"] ] ], "minimum_should_match"=>1, // should条件匹配⼀个⾜矣
必须放在 should后⾯
] ];sql: where class_name=“包包” and shop_id >1 and( shop_date>2020-01-01 or (shop_title=“裤⼦” and shop_price < 20))$body = [ "bool"=>[ "must"=>[ [ "range"=>[ "shop_id"=>["gte"=>1] ] ], [ "match"=>[ "class_name"=>"包包" ] ] ], "should"=>[ [ "range"=>[ "shop_date"=>["gte"=>"2020-12-27"] ] ], [ "bool"=>[ "must"=>[ [ "range"=>[ "shop_price"=>["gte"=>20] ] ], [ "match"=>[ "shop_title"=>"裤⼦" ] ] ], ] ] ], "minimum_should_match"=>1, ] ];sql :where shop_num is null$body = [ "bool"=>[ "must_not"=>[ "exists"=>["field"=>"shop_num"] ], ] ];3. 分页及排序排序:elasticsearch 默认排序 是_score降序。⾃⼰设定排序:必须放在body⾥ 语法格式:如下 public function search_file($index_name,$type_name,$body){ $params = [ 'index' => $index_name, 'type' => $type_name, 'body'=> [ 'query'=>$body, "sort"=>[ "shop_id"=>["order"=>"desc"] ] ], "from"=>1, "size"=>6, ]; $response = $this->client->search($params); return $response; } //
多个排序写法 "sort"=>[ ["shop_id"=>["order"=>"desc"]], ["shop_date"=>["order"=>"desc"]] ]分页:elasticsearch 默认每页10条 from:第⼏个⽂件开始 size:分页的⽂件数。
发布评论