1.设置mapping信息:
PUT /accounts?error_trace=true
{
"mappings": {
"properties": {
"patient": {
"type": "nested",
"properties": {
"type": {
"type": "keyword"
},
"account_number": {
"type": "long"
},
"balance": {
"type": "double"
},
"firstname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"lastname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"age": {
"type": "long"
},
"gender": {
"type": "keyword"
},
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"country": {
"type": "keyword"
}
}
}
}
}
}
2.批量灌数据:
1).脚本(Laravel):
public function handle(\Faker\Generator $faker)
{
echo "开始:";
$numb =0;
$i = 0;
while($numb < 200) {
$numb++;
$startTime = microtime(true);
$wPath = storage_path("app/account/accounts{$numb}.json");
$file = fopen($wPath, "w");
$i = 10000 * ($numb-1);
$k = $numb * 10000;
echo "$i,$k \r\n";
while ($i < $k) //2000000
{
$i++;
$id = [
"index" => [
"_id" => $i
]
];
fputs($file, json_encode($id));
fputs($file, PHP_EOL);
$rand = rand(1, 5);
$total = [];
$type = [0 => 'inpatient', 1 => 'outpatient'];
$gender = [0 => 'M', 1 => 'F'];
$country = [0 => 'France', 1 => 'England', 2 => 'Japan', 3 => 'America', 4 => 'Australia'];
$one = [
"firstname" => $faker->firstName,
"lastname" => $faker->lastName,
"address" => $faker->address,
"email" => $faker->email,
"city" => $faker->city,
"country" => $country[array_rand($country, 1)]
];
for ($j = 0; $j <= $rand; $j++) {
$one["account_number"] = rand(1, 1000000);
$one["balance"] = rand(1, 50);
$one["age"] = rand(10, 88);
$one["gender"] = $gender[array_rand($gender, 1)];
$one["type"] = $type[array_rand($type, 1)];
$total['patient'][] = $one;
}
fputs($file, json_encode($total));
fputs($file, PHP_EOL);
}
fclose($file);
}
$endTime = microtime(true);
echo $endTime - $startTime;
}
2).shell脚本灌数据(本地 linux 内存不够,数据量太大,会报内存溢出异常,所以使用分文件导入):
#!/bin/sh
i=1
while [ $i -le 200 ]
do
curl -H "Content-Type: application/json" -XPOST "http://192.168.75.206:9200/accounts/patient/_bulk?pretty&refresh" --data-binary @/home/data/account/accounts$i.json
i=$(( $i+1 ))
done
注:如果是单个文件直接使用:
curl -H "Content-Type: application/json" -XPOST "http://192.168.75.206:9200/accounts/_bulk?pretty&refresh" --data-binary @/home/data/accounts.json
3.搜索:
1).搜索全部:
GET /accounts/_search?error_trace=true
{
"size":10,
"query": {
"match_all": {
}
}
}
2).bool 搜索:
GET /accounts/_search?error_trace=true
{
"size": 10,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "patient",
"query": {
"bool": {
"must": [
{
"match": {
"patient.country": "Japan"
}
}
]
}
}
}
}
]
}
}
}
3).带聚合的搜索:
GET /accounts/_search?error_trace=true
{
"size": 10,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "patient",
"query": {
"bool": {
"must": [
{
"match": {
"patient.country": "Japan"
}
}
]
}
}
}
}
]
}
},
"aggs": {
"patient": {
"nested": {
"path": "patient"
},
"aggs": {
"group_by_type": {
"terms": {
"field": "patient.type"
}
}
}
}
}
}