Create document with pre-defined geo_shapes in Elasticsearch











up vote
0
down vote

favorite












I have two indices:




  1. Profiles - Has a field which is an array of geo_shape

  2. Locations - Has a field geometry which is a geo_shape


Each document in profiles has many locations. Currently, a copy of locations' geometry is stored in profile documents.



Is it possible to improve this by creating a profile document with pre-defined geo_shapes? I've tried



PUT profiles/profile/1
{
"locations": [
{
"indexed_shape": {
"id": "LOC1",
"index": "locations",
"path": "geometry",
"type": "location"
}
},
{
"indexed_shape": {
"id": "LOC2",
"index": "locations",
"path": "geometry",
"type": "location"
}
}
]
}


which is much like the query syntax for pre-defined geo shapes, but to no avail. I can't find anything in the docs. Is there a solution to this problem, or do I have to manage copies?










share|improve this question















This question has an open bounty worth +100
reputation from Dan Andreasson ending in 6 days.


This question has not received enough attention.
















  • The real question is what do you need to do with those locations stored in your profiles index?
    – Val
    Nov 21 at 14:49












  • @Val This is a simplified example, but I need to both search for profiles which have overlapping polygons (geo_shape really, could be geo collection) or contains a point. I've thought about having a separate index called profile_locations which mirrors my Postgres table. Not sure if that makes more sense? Still, have the same problem with managing copies tho
    – Dan Andreasson
    Nov 21 at 14:55






  • 1




    Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index. Then when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.
    – Val
    18 hours ago












  • Okay, thanks for the suggestion, much appreciated!
    – Dan Andreasson
    17 hours ago










  • @Val a quick question. What about when I want to search for profiles within a polygon and other criteria that lives on the profile? Is that somehow possible if I introduce a profile_locations index?
    – Dan Andreasson
    17 hours ago















up vote
0
down vote

favorite












I have two indices:




  1. Profiles - Has a field which is an array of geo_shape

  2. Locations - Has a field geometry which is a geo_shape


Each document in profiles has many locations. Currently, a copy of locations' geometry is stored in profile documents.



Is it possible to improve this by creating a profile document with pre-defined geo_shapes? I've tried



PUT profiles/profile/1
{
"locations": [
{
"indexed_shape": {
"id": "LOC1",
"index": "locations",
"path": "geometry",
"type": "location"
}
},
{
"indexed_shape": {
"id": "LOC2",
"index": "locations",
"path": "geometry",
"type": "location"
}
}
]
}


which is much like the query syntax for pre-defined geo shapes, but to no avail. I can't find anything in the docs. Is there a solution to this problem, or do I have to manage copies?










share|improve this question















This question has an open bounty worth +100
reputation from Dan Andreasson ending in 6 days.


This question has not received enough attention.
















  • The real question is what do you need to do with those locations stored in your profiles index?
    – Val
    Nov 21 at 14:49












  • @Val This is a simplified example, but I need to both search for profiles which have overlapping polygons (geo_shape really, could be geo collection) or contains a point. I've thought about having a separate index called profile_locations which mirrors my Postgres table. Not sure if that makes more sense? Still, have the same problem with managing copies tho
    – Dan Andreasson
    Nov 21 at 14:55






  • 1




    Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index. Then when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.
    – Val
    18 hours ago












  • Okay, thanks for the suggestion, much appreciated!
    – Dan Andreasson
    17 hours ago










  • @Val a quick question. What about when I want to search for profiles within a polygon and other criteria that lives on the profile? Is that somehow possible if I introduce a profile_locations index?
    – Dan Andreasson
    17 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have two indices:




  1. Profiles - Has a field which is an array of geo_shape

  2. Locations - Has a field geometry which is a geo_shape


Each document in profiles has many locations. Currently, a copy of locations' geometry is stored in profile documents.



Is it possible to improve this by creating a profile document with pre-defined geo_shapes? I've tried



PUT profiles/profile/1
{
"locations": [
{
"indexed_shape": {
"id": "LOC1",
"index": "locations",
"path": "geometry",
"type": "location"
}
},
{
"indexed_shape": {
"id": "LOC2",
"index": "locations",
"path": "geometry",
"type": "location"
}
}
]
}


which is much like the query syntax for pre-defined geo shapes, but to no avail. I can't find anything in the docs. Is there a solution to this problem, or do I have to manage copies?










share|improve this question













I have two indices:




  1. Profiles - Has a field which is an array of geo_shape

  2. Locations - Has a field geometry which is a geo_shape


Each document in profiles has many locations. Currently, a copy of locations' geometry is stored in profile documents.



Is it possible to improve this by creating a profile document with pre-defined geo_shapes? I've tried



PUT profiles/profile/1
{
"locations": [
{
"indexed_shape": {
"id": "LOC1",
"index": "locations",
"path": "geometry",
"type": "location"
}
},
{
"indexed_shape": {
"id": "LOC2",
"index": "locations",
"path": "geometry",
"type": "location"
}
}
]
}


which is much like the query syntax for pre-defined geo shapes, but to no avail. I can't find anything in the docs. Is there a solution to this problem, or do I have to manage copies?







elasticsearch gis






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 13:24









Dan Andreasson

5,03831924




5,03831924






This question has an open bounty worth +100
reputation from Dan Andreasson ending in 6 days.


This question has not received enough attention.








This question has an open bounty worth +100
reputation from Dan Andreasson ending in 6 days.


This question has not received enough attention.














  • The real question is what do you need to do with those locations stored in your profiles index?
    – Val
    Nov 21 at 14:49












  • @Val This is a simplified example, but I need to both search for profiles which have overlapping polygons (geo_shape really, could be geo collection) or contains a point. I've thought about having a separate index called profile_locations which mirrors my Postgres table. Not sure if that makes more sense? Still, have the same problem with managing copies tho
    – Dan Andreasson
    Nov 21 at 14:55






  • 1




    Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index. Then when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.
    – Val
    18 hours ago












  • Okay, thanks for the suggestion, much appreciated!
    – Dan Andreasson
    17 hours ago










  • @Val a quick question. What about when I want to search for profiles within a polygon and other criteria that lives on the profile? Is that somehow possible if I introduce a profile_locations index?
    – Dan Andreasson
    17 hours ago


















  • The real question is what do you need to do with those locations stored in your profiles index?
    – Val
    Nov 21 at 14:49












  • @Val This is a simplified example, but I need to both search for profiles which have overlapping polygons (geo_shape really, could be geo collection) or contains a point. I've thought about having a separate index called profile_locations which mirrors my Postgres table. Not sure if that makes more sense? Still, have the same problem with managing copies tho
    – Dan Andreasson
    Nov 21 at 14:55






  • 1




    Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index. Then when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.
    – Val
    18 hours ago












  • Okay, thanks for the suggestion, much appreciated!
    – Dan Andreasson
    17 hours ago










  • @Val a quick question. What about when I want to search for profiles within a polygon and other criteria that lives on the profile? Is that somehow possible if I introduce a profile_locations index?
    – Dan Andreasson
    17 hours ago
















The real question is what do you need to do with those locations stored in your profiles index?
– Val
Nov 21 at 14:49






The real question is what do you need to do with those locations stored in your profiles index?
– Val
Nov 21 at 14:49














@Val This is a simplified example, but I need to both search for profiles which have overlapping polygons (geo_shape really, could be geo collection) or contains a point. I've thought about having a separate index called profile_locations which mirrors my Postgres table. Not sure if that makes more sense? Still, have the same problem with managing copies tho
– Dan Andreasson
Nov 21 at 14:55




@Val This is a simplified example, but I need to both search for profiles which have overlapping polygons (geo_shape really, could be geo collection) or contains a point. I've thought about having a separate index called profile_locations which mirrors my Postgres table. Not sure if that makes more sense? Still, have the same problem with managing copies tho
– Dan Andreasson
Nov 21 at 14:55




1




1




Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index. Then when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.
– Val
18 hours ago






Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index. Then when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.
– Val
18 hours ago














Okay, thanks for the suggestion, much appreciated!
– Dan Andreasson
17 hours ago




Okay, thanks for the suggestion, much appreciated!
– Dan Andreasson
17 hours ago












@Val a quick question. What about when I want to search for profiles within a polygon and other criteria that lives on the profile? Is that somehow possible if I introduce a profile_locations index?
– Dan Andreasson
17 hours ago




@Val a quick question. What about when I want to search for profiles within a polygon and other criteria that lives on the profile? Is that somehow possible if I introduce a profile_locations index?
– Dan Andreasson
17 hours ago












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index, like this:



PUT shapes/doc/LOC1
{
... shape definition goes here ...
}
PUT shapes/doc/LOC2
{
... shape definition goes here ...
}

PUT profiles/doc/1
{
"locations": [ "LOC1", "LOC2" ]
... other fields
}


Then, when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.



First, query the shapes and gather the ids:



POST shapes/_search?filter_path=hits.hits._id
{
"query" : {
"geo_shape": {
"location": {
"shape": {
"type": "envelope",
"coordinates" : [[13.0, 53.0], [14.0, 52.0]]
},
"relation": "within"
}
}
}
}

=> returns "LOC1", "LOC3", "LOC4"


Finally, query the profiles index



POST profiles/_search
{
"query": {
"bool": {
"filter": [
{
...other profile criteria go here...
},
{
"terms": {
"locations": ["LOC1", "LOC3", "LOC4" ]
}
}
]
}
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413042%2fcreate-document-with-pre-defined-geo-shapes-in-elasticsearch%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index, like this:



    PUT shapes/doc/LOC1
    {
    ... shape definition goes here ...
    }
    PUT shapes/doc/LOC2
    {
    ... shape definition goes here ...
    }

    PUT profiles/doc/1
    {
    "locations": [ "LOC1", "LOC2" ]
    ... other fields
    }


    Then, when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.



    First, query the shapes and gather the ids:



    POST shapes/_search?filter_path=hits.hits._id
    {
    "query" : {
    "geo_shape": {
    "location": {
    "shape": {
    "type": "envelope",
    "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
    },
    "relation": "within"
    }
    }
    }
    }

    => returns "LOC1", "LOC3", "LOC4"


    Finally, query the profiles index



    POST profiles/_search
    {
    "query": {
    "bool": {
    "filter": [
    {
    ...other profile criteria go here...
    },
    {
    "terms": {
    "locations": ["LOC1", "LOC3", "LOC4" ]
    }
    }
    ]
    }
    }
    }





    share|improve this answer



























      up vote
      1
      down vote













      Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index, like this:



      PUT shapes/doc/LOC1
      {
      ... shape definition goes here ...
      }
      PUT shapes/doc/LOC2
      {
      ... shape definition goes here ...
      }

      PUT profiles/doc/1
      {
      "locations": [ "LOC1", "LOC2" ]
      ... other fields
      }


      Then, when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.



      First, query the shapes and gather the ids:



      POST shapes/_search?filter_path=hits.hits._id
      {
      "query" : {
      "geo_shape": {
      "location": {
      "shape": {
      "type": "envelope",
      "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
      },
      "relation": "within"
      }
      }
      }
      }

      => returns "LOC1", "LOC3", "LOC4"


      Finally, query the profiles index



      POST profiles/_search
      {
      "query": {
      "bool": {
      "filter": [
      {
      ...other profile criteria go here...
      },
      {
      "terms": {
      "locations": ["LOC1", "LOC3", "LOC4" ]
      }
      }
      ]
      }
      }
      }





      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index, like this:



        PUT shapes/doc/LOC1
        {
        ... shape definition goes here ...
        }
        PUT shapes/doc/LOC2
        {
        ... shape definition goes here ...
        }

        PUT profiles/doc/1
        {
        "locations": [ "LOC1", "LOC2" ]
        ... other fields
        }


        Then, when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.



        First, query the shapes and gather the ids:



        POST shapes/_search?filter_path=hits.hits._id
        {
        "query" : {
        "geo_shape": {
        "location": {
        "shape": {
        "type": "envelope",
        "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
        },
        "relation": "within"
        }
        }
        }
        }

        => returns "LOC1", "LOC3", "LOC4"


        Finally, query the profiles index



        POST profiles/_search
        {
        "query": {
        "bool": {
        "filter": [
        {
        ...other profile criteria go here...
        },
        {
        "terms": {
        "locations": ["LOC1", "LOC3", "LOC4" ]
        }
        }
        ]
        }
        }
        }





        share|improve this answer














        Unfortunately, there is no way (yet) to store references to indexed shapes. What you could do is to pre-index shapes into a dedicated index and then store the IDs of those shapes in the locations array in the profiles index, like this:



        PUT shapes/doc/LOC1
        {
        ... shape definition goes here ...
        }
        PUT shapes/doc/LOC2
        {
        ... shape definition goes here ...
        }

        PUT profiles/doc/1
        {
        "locations": [ "LOC1", "LOC2" ]
        ... other fields
        }


        Then, when you need to query, you can first do the query on the shapes index, gather the ids of the matching shapes and then query the profiles index using those ids. I don't see a way to make it shorter than that.



        First, query the shapes and gather the ids:



        POST shapes/_search?filter_path=hits.hits._id
        {
        "query" : {
        "geo_shape": {
        "location": {
        "shape": {
        "type": "envelope",
        "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
        },
        "relation": "within"
        }
        }
        }
        }

        => returns "LOC1", "LOC3", "LOC4"


        Finally, query the profiles index



        POST profiles/_search
        {
        "query": {
        "bool": {
        "filter": [
        {
        ...other profile criteria go here...
        },
        {
        "terms": {
        "locations": ["LOC1", "LOC3", "LOC4" ]
        }
        }
        ]
        }
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 12 hours ago

























        answered 17 hours ago









        Val

        98.5k6125163




        98.5k6125163






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413042%2fcreate-document-with-pre-defined-geo-shapes-in-elasticsearch%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks