我收到一个OCR应用程序的响应,它是一个类似于Google Vision OCR响应的JSON。我想把这个放在Elasticsearch中,以便执行形状查询。虽然我能够将JSON放入Elasticsearch中,但我无法想出一个完美的schema
和mapping
来基于形状或boundingPoly
和bgColor
进行搜索。
我是Elasticsearch的新手,我有一些问题。
(1(如何在boundingPoly
和bgColor
的基础上执行搜索?
(2(我需要更改执行搜索的模式吗?还是可以保持原样?什么是适合我目的的最佳模式和映射?
(3(此外,我想在bgColor
的基础上进行搜索。我怎样才能做到这一点?
我尝试了Geo-shape查询,但未能实现,结果正确。此外,
Geo-shape query
中有一个限制,即值必须介于90-180之间。我认为我们可以通过规范化值来处理这一部分。
示例JSON:
{
"responses": [{
"textAnnotations": [{
"description": "were",
"boundingPoly": {
"vertices": [{
"x": 112,
"y": 5
},
{
"x": 333,
"y": 5
},
{
"x": 333,
"y": 93
},
{
"x": 112,
"y": 93
}
],
"confidence": 99
},
"wordInfo": {
"length": 4,
"width": 221,
"height": 88,
"bgColor": [
255,
255,
251
]
}
},
{
"description": "gonna",
"boundingPoly": {
"vertices": [{
"x": 338,
"y": 5
},
{
"x": 589,
"y": 5
},
{
"x": 589,
"y": 93
},
{
"x": 338,
"y": 93
}
],
"confidence": 99
},
"wordInfo": {
"length": 5,
"width": 251,
"height": 88,
"bgColor": [
255,
255,
255
]
}
}
]
}]
}
提前感谢!
当您在图像上使用单词的边界框坐标时,我建议您使用shape数据类型,因为坐标值没有限制。
"vertices": {
"type": "shape"
}
此外,请确保以以下格式操作边界框坐标。
[[x1,y1],[x2,y2],[x3,y3],[x4,y4],[x1,y1]]
您可能希望在elasticsearch中更改模式,因为搜索文档中的不同字段会更容易。
将数据发布到文档中,如:
POST /example/_doc
{
"vertices" : {
"type" : "polygon",
"coordinates" : [
[ [1000.0, -1001.0], [1001.0, -1001.0], [1001.0, -1000.0], [1000.0, -1000.0], [1000.0, -1001.0] ]
]
}
}
对于搜索,你可以使用信封类型的查询,这样你就不必写边界框的所有坐标,你可以设置一个要搜索的信封(矩形(,它会给你信封中包含的所有文档。注意:包络类型搜索的坐标输入有点不同,它采用[[minX,maxY],[maxX,minY]]
这种类型的格式。
示例:
{
"query":{
"bool": {
"must": {
"match_all": {}
},
"filter": {
"shape": {
"prefix.vertices": {
"shape": {
"type": "envelope",
"coordinates": [
[40,60],
[100,40]
]
}
}
}
}
}
}
}
由于elasticsearch中的每个字段都可以有一个或多个(以数组的形式(值,因此可以使用must-match查询来搜索bgColor,以便将所有值匹配到bgColor元素。
示例:
{
"query": {
"bool": {
"must": [
{"match": {"prefix.wordInfo.bgColor": 1}},
{"match": {"prefix.wordInfo.bgColor": 2}},
{"match": {"prefix.wordInfo.bgColor": 3}}
]
}
}
}
我希望这能有所帮助。
geo_shape
适用于。。。地理形状。所以你必须选择
- 规范化您的坐标,使其符合lat/long规范的限制,然后才同步它们
- 或者,您可以将它们视为处于Mercator(EPSG:900913(投影中,该投影支持[589,93]等值,然后将它们转换为实际的纬度/经度。尽管你应该记住,比如说,转换为mercator坐标的地球点[0(lon(,1(lat(]是[0,~111325]。因此,你还需要一些比例因子,以免使你的地缘多边形太小。换句话说,在WGS 84中,1 mercator坐标的差异为0.000009°,这意味着多边形可能太小而无法索引和/或分辨率太高而无法有效存储
分步指南:
- 转换和/或规范化坐标
- 将[x,y]对转换为[lon,lat]对
- 将您的[r,g,b]颜色转换为rgb字符串([0,0,0]=>"#000000"(——除非您打算通过实际的r,g、b通道进行查询
- 创建带几何图形映射的索引:
PUT /example
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
},
"bgColor": {
"type": "keyword"
}
}
}
}
- 确保多边形坐标以相同的地理点开始和结束&将它们封装在另外两个空数组中,以符合geojson标准
- 同步多边形+
bgColor
s
POST /example/_doc
{
"location" : {
"type" : "polygon",
"coordinates" : [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
},
"bgColor": "#000000"
}
在那之后,关于如何搜索多边形以及如何搜索(颜色(字符串,其他地方已经有了大量的资源。