我的 Elasticsearch 节点中有一个文档,其中包含有关某个人的联系人的信息:
"contacts": [
{
"id": 1,
"address": "address1",
"tel": "40071322",
"doctor_id": 1,
"type_id": 1,
"lng": "51.374720",
"lat": "35.781986",
"city_id": 186,
"province_id": 8,
"hour_about": null,
"place_name": null
},
{
"id": 2,
"address": "address2",
"tel": null,
"doctor_id": 1,
"type_id": 2,
"lng": "51.520313",
"lat": "35.726983",
"city_id": 186,
"province_id": 8,
"hour_about": null,
"place_name": null
},
{
"id": 3,
"address": "address3",
"tel": null,
"doctor_id": 1,
"type_id": 2,
"lng": "51.456368",
"lat": "35.797505",
"city_id": 186,
"province_id": 8,
"hour_about": null,
"place_name": null
}
]
我想运行
term
过滤器来查找在city_id
字段中具有X
的contacts
的文档。当字段本身具有上述子字段时,该如何查询?最好使用
elasticsearch php api
=)谢谢你的帮助。
最佳答案
无需使用任何插件即可完成此操作。
<?php
$jsondata = '{"contacts":[
{"id":1,"address":"address1","tel":"40071322","doctor_id":1,"type_id":1,"lng":"51.374720","lat":"35.781986","city_id":186,"province_id":8,"hour_about":null,"place_name":null},
{"id":2,"address":"address2","tel":null,"doctor_id":1,"type_id":2,"lng":"51.520313","lat":"35.726983","city_id":186,"province_id":8,"hour_about":null,"place_name":null},
{"id":3,"address":"address3","tel":null,"doctor_id":1,"type_id":2,"lng":"51.456368","lat":"35.797505","city_id":186,"province_id":8,"hour_about":null,"place_name":null}
]}';
$jsonArray = json_decode($jsondata, true);
echo ('<pre>');
print_r ($jsonArray);
echo ('</pre>');
foreach($jsonArray["contacts"] as $person) {
if($person['city_id'] == 186) {
echo "id = " . $person['id'];
echo "<br/>";
}
}
?>
$jsondata
存储JSON,您也可以通过磁盘上的
$jsondata = file_get_contents ('./contacts.json');
$jsondata = file_get_contents ('http://www.example.com/contacts.json');
json_decode
将JSON解码为一个关联数组,并将其存储在变量$jsonArray
中,然后进行打印。使用
for
语句在if
循环中进行比较。现在在这种情况下,由于所有
city_id
的值都相同(= 186),因此将打印所有三个对应的id
值。演示here。
关于php - 如何在Elasticsearch中具有子字段的字段上运行术语过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29984634/