弹性搜索响应

时间:2015-10-14 18:17:30

标签: elasticsearch field geopoints

如果使用" fields []"

指定,结果中的geo_point字段似乎会被忽略

我有索引test01的以下映射

{
   "test01": {
      "mappings": {
         "activity": {
            "properties": {
               "location": {
                  "type": "string"
               },
               "mygeo": {
                  "type": "geo_point",
                  "doc_values": true,
                  "fielddata": {
                     "format": "compressed",
                     "precision": "1km"
                  }
               }
            }
         }
      }
   }
}

索引包含单个活动

{
      "mygeo": {
        "lat": 51.247607909,
        "lon": 22.565701278
      },
      "location" : "New York"
}

查询

GET /test01/_search
{
  "size" : 1,
  "fields": ["location", "mygeo"]
}

生成以下缺少mygeo字段的位置。 (我也试过"字段":["位置"," mygeo.lat"," mygeo.lon"," mygeo"])。

 "hits": [
     {
        "_index": "test01",
        "_type": "activity",
        "_id": "1",
        "_score": 1,
        "fields": {
           "location": [
              "New York"
           ]
        }
     }
  ]

我能获得mygeo对象的唯一方法是通过添加" _source"来通过_source。 :{"包括" :[" mygeo" ]}。

有没有办法使用"字段"来获取geo_point字段?参数β

我已经尝试过Rest API和Java API。两者都使用Elasticsearch v.1.7.1产生相同的结果。

谢谢

2 个答案:

答案 0 :(得分:2)

因此,按照逻辑,我已经通过向您的索引映射添加"store": true来复制并解决了问题,现在它允许我使用fields检索lon / lat,而不是{{ 1}}。

请参阅我的localhost上的Sense上的复制:

_source

因此,此查询确实会带来您期望的结果。唯一的问题是你的lan / lon被格式化为一个数组。查看查询结果:

DELETE test

PUT /test
{
  "mappings": {
    "test1": {
      "properties": {
        "location": {
          "type": "string"
        },
        "mygeo": {
          "type": "geo_point",
          "doc_values": true,
          "store": true, 
          "fielddata": {
            "format": "compressed",
            "precision": "1km"
          }
        }
      }
    }
  }
}

POST /test/test1/
{
  "mygeo": {
    "lat": 51.247607909,
    "lon": 22.565701278
  },
  "location": "New York"
}


GET /test/_search
{
  "size" : 1,
  "fields": ["location", "mygeo"]
}

然而,这是Elasticsearch官方支持的格式之一。取自documentation

  

将location字段定义为geo_point,我们可以继续   包含纬度/经度对的索引文档,可以是   格式化为字符串,数组或对象:

{
  "hits" : [{
      "_index" : "test",
      "_type" : "test1",
      "_id" : "AVCGqhsq9y2W0mh1rPgV",
      "_score" : 1,
      "fields" : {
        "mygeo" : [
          "51.247607909,22.565701278"
        ],
        "location" : [
          "New York"
        ]
      }
    }
  ]
}

另外,请在文档中注明:

  

每个人都至少被抓到一次:字符串地理位置是   "纬度,经度",而数组地理点是   [经度,纬度] - 相反的顺序!

答案 1 :(得分:1)

如果您的字段存储在_source中(即使它是GeoPoint类型字段),您必须使用“_source”查询参数来指定您想要的字段(来源):

GET /_search
{
    "_source": "obj.*",
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

在此处查看更多详情:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

长话短说:

  • 如果您的字段位于源代码中,则需要在查询中指定“_source”模式以仅获取所需的部分(字段)
  • 在查询中指定“fields”将(通常)对于使用“store:yes”在映射中声明的字段有意义。这并没有说明应该从源返回哪些字段。
  • 是的,这里有一个不一致的地方:如果您的字段在源中映射,但未使用“Store:yes”声明,那么在查询的“fields”参数中指定它可能会也可能不会返回它(我已经注意到在这种情况下会返回“简单”字段,例如文本,数字等,但是更复杂的字段(例如时间戳或GeoPoint)不会。)
相关问题