ES 搜索相关整理

控制台命令

#查看索引文档总数
GET /pre_video/_count

#查看配置
GET /pre_video/_settings

#查看索引信息
GET /pre_video/_stats 

#查看所有节点索引占用空间
GET /_cat/shards

#查看节点详细信息
GET _nodes/stats

#查看内存占用情况
GET /_cat/nodes?h=name,fm,fcm,sm,qcm,im&v

#设置最大条数限制
PUT /pre_video/_settings?preserve_existing=true
{
  "max_result_window": 20000
}

#清空索引文档
POST /pre_video/_delete_by_query?pretty
{
    "query": {
       "match_all": {}
     }
}

#排序并取一条
GET /pre_video/_doc/_search
{
  "sort": [
    {
      "id": {
        "order": "desc"
      }
    }
  ],
  "size":1
}

#查询文档
POST /pre_video/_doc/_search
{
 "track_total_hits" : true, //放开10000条文档限制
 "query" : {
        "match" : {
            "collect_type" : 6
        }
    }
}

#分页查询,限制10000之内,浅分页
GET /pre_video/_doc/_search
{
    "from" : 5, 
    "size" : 1,
    "query" : {
       "match_all": {}
    }
}


#分页查询,超过10000限制,深分页
POST /pre_video/_doc/_search
{
  "track_total_hits" : true, //放开10000条文档限制
  "query" : {
        "match" : {
            "collect_type" : 6
        }
   },
   "sort": [
      {
        "id": {
          "order": "desc"
        }
      }
   ],
   'search_after' => [''], //上次 Sort field ID
}

#更新文档
POST /pre_video/_doc/4/_update
{
  "doc":{
      "collect_desc":"急刹车"
   }
}

#插入文档
PUT /test/_doc/3
{
"name" : "testetst",
"desc" : "testets-One1",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
 
#POST 可不用指定ID自动生成
POST /test/_doc/
{
"name" : "testet",
"desc" : "aaass-One1",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}

#删除文档
DELETE /pre_video/_doc/4

#删除索引
DELETE /pre_video/

#查看指定ID分布分片位置
GET /pre_video/_search_shards?routing=ID

#创建索引
PUT /adas_m01b_property_2021_05
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
    },
    "mappings": {
        "_source": {
            "enabled": true
        },
        "properties": {
            "id": {
                "type": "keyword"
            },
            "unique_id": {
                "type": "keyword"
            },
            "vin": {
                "type": "keyword"
            },
            "tag": {
                "type": "keyword"
            },
            "mid_latlng":{
                "type": "geo_point"
            },
            "city_code":{
                "type": "integer"
            },
            "scenario":{
                "type": "integer"
            },
            "trigger_type":{
                "type": "integer"
            },
            "mile":{
                "type": "keyword"
            },
            "gps_collect_id":{
                "type": "integer"
            },
            "perception_tag":{
                "type": "object"
            },
            "mtime": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            },
            "ctime": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            }
        }
    }
}

# 追加索引字段
POST adas_m01b_property_2021_05/_mappings?
{
  "properties":{
    "perception_tag":{
      "type":"object"
    }
  }
}

# 复杂查询 => 经纬度+时间过滤
GET pre_video_map/_doc/_search
{
  "track_total_hits": true,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "collect_time": {
              "gte": "1599435228464",
              "lte": "1599470232507"
            }
          }
        }
      ],
      "filter": {
        "geo_bounding_box": {
          "mid_latlng": {
            "top_left": {
              "lat": 53.490694,
              "lon": 120.282936
            },
            "bottom_right": {
              "lat": 42.972134,
              "lon": 134.41206
            }
          }
        }
      }
    }
  }

# 查找给定范围内的点
GET pre_video_map/_doc/_search
{
  "query": {
    "geo_distance": {
      "distance": "2000m",
      "mid_latlng": {
        "lat": 39.904309, //纬度
        "lon": 116.438764 //精度
      }
    }
  }
}

#查找两个点组成矩形内的点
GET pre_video_map/_doc/_search
{
  "query": {
    "geo_bounding_box": {
      "mid_latlng": {
        "top_left": {
          "lat": 39.989272,
          "lon": 116.297334
        },
        "bottom_right": {
          "lat": 39.894567,
          "lon": 116.468084
        }
      }
    }
  }
}
# 创建别名
POST /_aliases
{
    "actions" : [
        { "add" : { "indices" : ["adas_m01b_property_2021_03", "adas_m01b_property_2021_04"], "alias" : "alias1" } }
    ]
}
# 通配符形式
POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "adas_m01b_property_*", "alias" : "adas_m01b_property" } }
    ]
}
#删除别名
POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "adas_m01b_property_*", "alias" : "adas_m01b_property" } }
    ]
}
#查看别名
GET /adas_m01b_property/_alias

# distinct + count
GET pre_video_map/_doc/_search
{
  "track_total_hits": true,
  "query": {
    "term": {
        "vin":"LW433B107L1009661"
    }
  },
  "collapse":{
    "field":"vin"
  }
}
GET pre_video_map/_doc/_search
{
  "track_total_hits": true,
  "query": {
    "bool": {
      "must": {
        "term": {
          "city_code": "210200"
        }
      }
    }
  },
  "aggs": {
    "distinct_colors": {
      "cardinality": {
        "field": "vin"
      }
    }
  },
  "size": 0
}

#group by + sum
GET index_name/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_name": {
      "terms": {
        "field": "name",
        "size": 10000,
        "order": {
          "amount": "desc"
        }
      },
      "aggs": {
        "amount": {
          "sum": {
            "field": "amount"
          }
        }
      }
    }
  }
}

PHP-SDK

$params = [
    'index' => 'pre_video',
    'body' => [
        'settings' => [
            'number_of_shards' => 1,
            'number_of_replicas' => 0
        ],
        'mappings' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'id'  => [
                        'type' => 'integer',
                     ],
                    'vin' => [
                        'type' => 'keyword',  //[keyword=>不分词|text=>分词]
                        //'analyzer' => 'standard' //选择分词器
                    ],
                    'vid' => [
                        'type' => 'keyword',
                    ],
                    'collect_type' => [
                        'type' => 'integer',
                    ],
                    'collect_desc' => [
                        'type' => 'keyword',
                    ],
                    'collect_desc_ik' => [
                        'type' => 'text',
                    ],
                    'status' => [
                        'type' => 'integer',
                    ],
                    'ctime' => [
                        'type' => 'date',
                        "format" => "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
                    ]
                ]
            ]
        ]
];

$response = $ESClient->indices()->create($params);
print_r($response);exit;

//删除索引
$params   = ['index' => 'pre_video'];
$response = $ESClient->indices()->delete($params);

//删除文档
$params = [
    'index' => 'pre_video',
    'type'  => '_doc',
    'id'    => '123'
];
$response = $ESClient->delete($params);

//id文档查询
$params = array(
    'index' => 'pre_video',
    'type'  => '_doc',
    'id'    =>'208'
);
print_r($ESClient->search($params));

//检索条件查询
$params = [
    'index' => 'pre_video',
    'type' => '_doc',
    'body' => [
        "track_total_hits" => true,
        'query' => [
            'match' => [
                'collect_type' => 6
            ]
        ],
        'sort' => [
            "id" => "asc",
        ],
        'search_after' => ['581'], //深分页,上一次检索排序字段ID
        "from" => 10400, //浅分页
        "size" => 10,
    ]
];
print_r($ESClient->search($params));

//短语查询
$params = [
    'index' => 'pre_video',
    'type' => '_doc',
    'body' => [
        "track_total_hits" => true,
        'query' => [
            "match_phrase" => [
                'vin' => [
                    'query' => 'f1cd3f0f000000000',
                ]
            ],
        ]
    ]
];
print_r($ESClient->search($params));

//排序查询,获取最大ID
$params = [
    'index' => 'pre_video',
    'type' => '_doc',
    'body' => [
        "track_total_hits" => true,
        "query" => [
            'match_all' => new \stdClass(),
        ],
        "sort" => [
            "id" => array("order" => "desc"),
        ],
        "size" => 1,
    ]
];
print_r($ESClient->search($params));

//联合查询 Bool And
/*
    must     查询的结果必须匹配查询条件,并计算score
    filter   查询的结果必须匹配查询条件,和must不同不会计算score
    should   查询结果必须符合查询条件中的一个或多个
    must_not 查询结果必须不符合查询条件
    -------------------------------------------------------------------------------
    term     不会对查询对字段进行分词处理
    match    会对字段进行分词,所查询的字段数据只要包含分词后结果的一个,就会被查询到 
    match_phrase 短语匹配查询,必须匹配短语中的所有分词,并且保证各个分词的相对位置不变
    multi_match  查询多个字段包含某个关键词的数据 
    range   范围查找,查找某一范围的所有数据 gt:大于、gte:大于等于、lt:小于、lte:小于等于
*/
$params = [
    'index' => 'pre_video',
    'type' => '_doc',
    'body' => [
        "track_total_hits" => true,
        'query' => [
            'bool' => [
                'must' => [
                    [
                        'match' => [
                            'collect_desc_ik' => 'stop',
                        ]
                    ],
                    [
                        'match' => [
                            'id' => 143868,
                        ]
                    ]
                ]
            ]
        ],
    ]
];
$params = [
    'index' => 'pre_video',
    'type' => '_doc',
    'body' => [
        "track_total_hits" => true,
        'query' => [
            'bool' => [
                'filter' => [
                    'multi_match' => [
                         "query" => "刹车",
                         "fields" => ["collect_desc_ik", "collect_desc"]
                    ]
                ]
            ]
        ],
    ]
];
$params = [
    'index' => 'pre_video',
    'type' => '_doc',
    'body' => [
        "track_total_hits" => true,
        'query' => [
            'bool' => [
                'filter' => [
                    'range' => [
                        'times' => [
                            "gte" => "2019-08-10 10:08:29", 
                            "lte" => "2020-08-13 10:08:29"
                        ]
                    ]
                ]
            ]
        ],
    ]
];
print_r($ESClient->search($params));

你可能感兴趣的:(ES 搜索相关整理)