es:使用嵌套nested搜索

一,创建索引:

说明:创建索引时,要使用nested类型

      //创建索引库
      public function createSecondIndex($client,$indexName) {
          $params = [
              'index' => $indexName, //索引的名称(mysql的表名)
              'body' => [
                  'settings' => [
                      'number_of_shards' => 5,//设置数据的分片数,默认为5
                      'number_of_replicas' => 0,//备份数,单个es服务器无需备份
                  ],
                  'mappings' => [
                      'properties' => [

                          'id' => ['type' => 'long'],       //id
                          'city'=>['type' => 'keyword'],       //城市

                          'unit_list'=>[
                              'type' => 'nested',
                              'properties' => [
                                    'bed_rooms'=>['type'=>'integer'],
                                    'area'=>['type'=>'float'],
                                    'selling_price'=>['type'=>'long'],
                                    'unit_price'=>['type'=>'long'],
                               ]
                          ],

二,添加索引

            //得到unit_list:户型列表
            $unit_list = [];

            foreach($rowsUnit as $ku => $oneUnit){
                $one=[
                    'bed_rooms'=>$oneUnit['bed_rooms'],
                    'area'=>$oneUnit['area'],
                    'selling_price'=>$oneUnit['selling_price'],
                    'unit_price'=>$oneUnit['unit_price'],
                ];

                $unit_list[]=$one;
            }

三,搜索:

          if (isset($params['bedrooms']) && is_array($params['bedrooms'])) {
              $field_name = 'unit_list.bed_rooms';
              $arr_ar = $params['bedrooms'];
              $ar_should = [];
              $ar_should['bool']['should'] = [];

              foreach($arr_ar as $k => $one){

                  if (strpos($one, '-') === false) {    //没有-
                      //
                      //continue;
                      $one = trim($one);

                      //判断one是否数字,非数字则continue
                      if (is_numeric($one) == false) {
                          continue;
                      }


                      $oneWhere = ['term' => [$field_name =>$one]];
                      $ar_should['bool']['should'][] = $oneWhere;

                  } else {       //有-
                      $one_arr = explode('-', $one);
                      $begin = trim($one_arr[0]);
                      $end = trim($one_arr[1]);

                      //判断end是否数字,非数字则continue
                      if (is_numeric($end) == false) {
                          continue;
                      }

                      if ($begin == 'gte' || $begin == 'gt' || $begin == 'lte' || $begin == 'lt') {
                          $range = ['range' => [
                              $field_name => [
                                  $begin => $end     // lt 表示小于 lte 表示小于等于
                              ]
                          ]];
                          $ar_should['bool']['should'][] = $range;
                      }

                  }
              }


              $ret = ['nested'=>['path'=>'unit_list','query'=>$ar_should]];
              $mustWhere[] = $ret;

          }

 

posted @ 2024-09-14 10:19  刘宏缔的架构森林  阅读(35)  评论(0编辑  收藏  举报