Create document with pre-defined geo_shapes in Elasticsearch












0















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























  • The real question is what do you need to do with those locations stored in your profiles index?

    – Val
    Nov 21 '18 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 '18 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
    Nov 26 '18 at 10:56













  • Okay, thanks for the suggestion, much appreciated!

    – Dan Andreasson
    Nov 26 '18 at 11:31











  • @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
    Nov 26 '18 at 11:54
















0















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























  • The real question is what do you need to do with those locations stored in your profiles index?

    – Val
    Nov 21 '18 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 '18 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
    Nov 26 '18 at 10:56













  • Okay, thanks for the suggestion, much appreciated!

    – Dan Andreasson
    Nov 26 '18 at 11:31











  • @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
    Nov 26 '18 at 11:54














0












0








0








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 '18 at 13:24









Dan AndreassonDan Andreasson

5,45432025




5,45432025













  • The real question is what do you need to do with those locations stored in your profiles index?

    – Val
    Nov 21 '18 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 '18 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
    Nov 26 '18 at 10:56













  • Okay, thanks for the suggestion, much appreciated!

    – Dan Andreasson
    Nov 26 '18 at 11:31











  • @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
    Nov 26 '18 at 11:54



















  • The real question is what do you need to do with those locations stored in your profiles index?

    – Val
    Nov 21 '18 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 '18 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
    Nov 26 '18 at 10:56













  • Okay, thanks for the suggestion, much appreciated!

    – Dan Andreasson
    Nov 26 '18 at 11:31











  • @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
    Nov 26 '18 at 11:54

















The real question is what do you need to do with those locations stored in your profiles index?

– Val
Nov 21 '18 at 14:49







The real question is what do you need to do with those locations stored in your profiles index?

– Val
Nov 21 '18 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 '18 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 '18 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
Nov 26 '18 at 10:56







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
Nov 26 '18 at 10:56















Okay, thanks for the suggestion, much appreciated!

– Dan Andreasson
Nov 26 '18 at 11:31





Okay, thanks for the suggestion, much appreciated!

– Dan Andreasson
Nov 26 '18 at 11:31













@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
Nov 26 '18 at 11:54





@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
Nov 26 '18 at 11:54












1 Answer
1






active

oldest

votes


















1





+100









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',
    autoActivateHeartbeat: false,
    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









    1





    +100









    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






























      1





      +100









      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




























        1





        +100







        1





        +100



        1




        +100





        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 Nov 26 '18 at 16:40

























        answered Nov 26 '18 at 11:58









        ValVal

        104k6141175




        104k6141175






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]