首页 > 其他分享 >es:使用嵌套nested搜索

es:使用嵌套nested搜索

时间:2024-09-14 10:12:53浏览次数:1  
标签:begin list should 嵌套 ar es nested type unit

一,创建索引:

说明:创建索引时,要使用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;

          }

 

标签:begin,list,should,嵌套,ar,es,nested,type,unit
From: https://www.cnblogs.com/architectforest/p/18408750

相关文章

  • 论文分享 《Timing Side-channel Attacks and Countermeasures in CPU Microarchitect
    Attack概述传统攻击(CONVENTIONALATTACKS)在传统攻击中,Attacker通常:与Victim共享硬件资源(比如说LLC,BP,Prefetcher等)可以观察,改变微架构状态攻击步骤本文作者将传统攻击分为以下三步,如Fig1所示:定位“漏洞”:该漏洞包括“代码漏洞”(vulnerablecodegadgets),即......
  • PbootCMS附件上传报错UNKNOW: Code: 8192; Desc: stripos()
    这个问题似乎是由于 stripos() 函数在处理某些字符或字符串时出现了不兼容或者错误的情况。然而,建议的修改方式 stripos($types,chr($ext)) 看起来并不正确,因为 chr() 函数会将一个ASCII值转换成对应的字符,而 $ext 通常是一个表示文件扩展名的字符串。解决方案检......
  • AV1 Bitstream & Decoding Process Specification--[4]:语法结构
    原文地址:https://aomediacodec.github.io/av1-spec/av1-spec.pdf没有梯子的下载地址:AV1Bitstream&DecodingProcessSpecification摘要:这份文档定义了开放媒体联盟(AllianceforOpenMedia)AV1视频编解码器的比特流格式和解码过程。规范:此文档规定了开放媒体联盟(Alliance......
  • WordPress加载流程的解读分析
    index.php```php<?php/**这个文件只用来加载'/wp-blog-header.php'**@packageWordPress//**声明一个全局变量,用来判断是否加载主题**@varbool/define('WP_USE_THEMES',true);/*加载WordPress环境和模板/requireDIR.'/wp-blog-header.php';```wp......
  • lesson05-设计主函数实现串口的初始化和运行
    根据boot.S里的内容,最后会跳转到kernel_main里,在这里会进行设置gpio引脚配置串口的初始化,然后循环运行,不断打印接受到的内容。//主函数voidkernel_main(){//串口初始化uart_init();//串口发送helloworlduart_send_string("Helloword!\n")//循环发......
  • lesson04-设计初始化bss段、读写寄存器值的汇编函数
    在内核启动时需要将bss段的所有数据清0,这里就需要memzero函数。.globalmemzero;全局可见memzero:strxzr,[x0],#8subsx1,x1,#8b.gtmemzeroret内核启动时需要经常读写soc内部寄存器的值,这里就需要用到对应的函数put32和get32。.globalput32......
  • 【IPV6从入门到起飞】5-2 IPV6+Home Assistant(ESP32+MQTT+DHT11+BH1750)传感器采集上
    IPV6+HomeAssistant[ESP32+MQTT+DHT11+BH1750]传感器采集上传监测1背景2实现效果3HomeAssistant配置3-1MQTT配置3-2yaml配置3-3加载配置4ESP32搭建4-1开发环境4-2工程代码5实现效果1背景在上一小节【IPV6从入门到起飞】5-1IPV6+HomeAssistant(搭建......
  • 物联网毕设 -- 图传种植监测控制(STM32+ESP32+APP)
    目录一连线图1原理图2PCB效果(面包版不适用)3实物效果4APP效果5功能概括(1)硬件端(2)APP端(4)云平台使用(需要可以找我获取)(5)演示视频二底层代码使用方式1.使用说明2.下载程序三APP使用方式1下载APP四程序架构及修改(通用) 前言该智能环境监测与控制系统......
  • Java 21的Process API的笔记
    JavaCoreLibrariesProcessAPIProcessAPIClassesandInterfacesProcessBuilderClass指定新进程的命令,工作路径,环境变量,指定输入和输出流即stdin/stdout/stderr。ProcessClass提供操作子进程的方法,比如等待子进程结束。获取进程的信息比如是否存活、进程ID......
  • JAVA Response 返回值再拿
    一:概述在现代Web开发中,与外部服务交互是一个常见的需求。Java作为一门广泛使用的编程语言,提供了多种方法来发送HTTP请求并处理响应。本文将探讨在Java中获取HTTP响应的不同方法,并提供实际案例。二:具体说明<1>使用java.net.HttpURLConnectionHttpURLConnection是Java标准库中一个古......