php - 如何在Elasticsearch中具有子字段的字段上运行术语过滤器

标签 php json api search elasticsearch

我的 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字段中具有Xcontacts的文档。当字段本身具有上述子字段时,该如何查询?

最好使用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/

    相关文章:

    php - 我如何提示用户下载 PHP 生成的 .CSV 文件,我应该如何处理删除它?

    php - 在 money_format 函数中出现 fatal error

    javascript - HTTP + JSON 简单 api 请求作为空白发送

    SQL:计算多项选择结果的答案

    c# - 使用 ServiceStack 增加 AuthSession

    android - Response.body() 在 Android 中使用 Retrofit 2.0.2 时返回具有空属性的对象

    python - 无法在 AWS Rest API 请求中使用 "Filter"

    php - 提交之前抓取记录 ID 并保存到另一个表

    php - 按顺序在 5 个项目后添加 div

    c# - JSON.Net 序列化程序忽略 JsonProperty?