Elasticsearch GeoMapping与PHP无法正常工作

时间:2016-06-26 21:20:49

标签: php laravel elasticsearch

我在Elasticsearch 2.2中有以下映射

  "clinics" : {
    "mappings" : {
      "clinic" : {
        "properties" : {
          "address_1" : {
            "type" : "string",
            "analyzer" : "standard"
          },
          "city" : {
            "type" : "string"
          },
          "country" : {
            "type" : "string"
          },
          "id" : {
            "type" : "long"
          },
          "location" : {
            "type" : "geo_point"
          },
          "name" : {
            "type" : "string",
            "analyzer" : "standard"
          },
          "state" : {
            "type" : "string"
          },
          "zipcode" : {
            "type" : "string",
            "analyzer" : "standard"
          }
        }
      }
    }
  },

我正在使用PHP通过Elastiquent5添加到索引中。我的toArray()是:

public function toArray()
{
    $array = parent::toArray();
    $object = new \stdClass();
    $object->lat = $array['latitude'];
    $object->lon = $array['longitude'];
    $array['location'] = $object;
    return $array;
}

当我尝试将项目添加到索引时,我收到以下错误:

{"error":{"root_cause":[{"type":"remote_transport_exception","reason":"[Solarman][10.0.2.15:9300][indices:data/write/index[p]]"}],"type":"illegal_argument_exception","reason":"[location] is defined as an object in mapping [clinics] but this name is already used for a field in other types"},"status":400}

以json格式正确生成,在php数组中它是:

array:10 [
  "name" => "Clinic A"
  "address_1" => "123 Main St"
  "city" => "NY"
  "state" => "NY"
  "zipcode" => "11111"
  "country" => "US"
  "id" => 6968
  "location" => {#526
    +"lat": 45.2116373
    +"lon": -72.1891546
  }
]

如果我手动json_encode这个并使用curl -XPUT 'http://localhost:9200' -d ''

它通过 - 然而PHP没有。我知道哪里出错了?

澄清 我的目标是插入我的模型数据提取的坐标以映射到索引内的位置字段,因为我已经设计为我的映射所定义的geo_point类型

我不想指定一个新字段来解决这个问题。次要说明:正如我所提到的那样,使用json的put会成功生成,但不会像php驱动程序那样用弹性搜索的php驱动程序在尝试创建文档时产生错误

1 个答案:

答案 0 :(得分:0)

如错误消息中所述,[location]被定义为映射[clinics]中的对象,但此名称已用于properties中的字段。如下所示更改变量名称可能有效。

public function toArray()
{
    $array = parent::toArray();
    $object = new \stdClass();
    $object->lat = $array['latitude'];
    $object->lon = $array['longitude'];
    $array['location_ob'] = $object;
    return $array;
}