【问题标题】:How to run term filter on a field which has child fields in Elasticsearch如何在 Elasticsearch 中具有子字段的字段上运行术语过滤器
【发布时间】:2015-07-11 03:40:41
【问题描述】:

我的 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 过滤器 来查找在其contacts 字段中包含city_idX 的文档。当字段本身有像上面这样的子字段时,我应该怎么做这个查询?

首选使用elasticsearch php api =)

感谢您的帮助。

【问题讨论】:

  • 到目前为止你都尝试过做什么?
  • @Ashesh,我试过'term' => array ( 'contacts' => intval($value) )
  • @Ashesh,我想我应该使用term 过滤器,对吗?导致它的 1 值
  • 包括您使用的映射。您是否使用嵌套文档或父子对象来映射联系人。查询将取决于您使用的映射类型。

标签: php json api search elasticsearch


【解决方案1】:

这可以在不使用任何插件的情况下完成。

<?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.

【讨论】:

    猜你喜欢
    • 2016-10-30
    • 2013-07-28
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多