Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:46:30+00:00 2026-06-09T10:46:30+00:00

I am reading the docs for elasticsearch and this [page][1] talks about mapping a

  • 0

I am reading the docs for elasticsearch and this [page][1] talks about mapping a child to a parent type using _parent.

If I have childs called email attached to parents called account:

Fields in each type:

account (http://localhost:9200/myapp/account/1)
========
id
name
some_other_info
state

email (http://localhost:9200/myapp/email/1?parent=1)
========
id
email
  • How can I search on the name field of account and the email field of email provided that the state of account is active?

  • Is there a way to get all the children (of a certain type or of any type) a parent owns?

  • When indexing a child document, is it possible to to pass the parent as an object property in the JSON data as opposed to it being part of the query string?


After trying imotov’s suggestion, I came up with this query:

This is executed on http://localhost:9200/myapp/account/_search

{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "name": "a"
          }
        },
        {
          "term": {
            "statuses": "active"
          }
        }
      ],
      "should": [
        {
          "has_child": {
            "type": "emailaddress",
            "query": {
              "prefix": {
                "email": "a"
              }
            }
          }
        }
      ]
    }
  }
}

The problem is that the above does not give me any accounts where the email matches.

The effect I want is essentially this:

  • There is one search box
  • Users start typing and the search box autocompletes.
  • The user’s query is checked against the name of the account or any of the emailaddress type.
  • If accounts were matched, just return them. If emailaddress were match, return its parent account.
  • Limit to a maximum of x (say 10) accounts for each search.

So, I basically need to be able to OR the search between 2 types and return the parent type of matches.


Test data:

curl -XPUT http://localhost:9200/test/account/1 -d '{
    "name": "John Smith",
    "statuses": "active"
}'

curl -XPUT http://localhost:9200/test/account/2 -d '{
    "name": "Peter Smith",
    "statuses": "active"
}'

curl -XPUT http://localhost:9200/test/account/3 -d '{
    "name": "Andy Smith",
    "statuses": "active"
}'

//Set up mapping for parent/child relationship

curl -XPUT 'http://localhost:9200/test/email/_mapping' -d '{
    "emails" : {
        "_parent" : {"type" : "account"}
    }
}'

curl -XPUT http://localhost:9200/test/email/1?parent=1 -d '{
    "email": "john@smith.com"
}'

curl -XPUT http://localhost:9200/test/email/2?parent=1 -d '{
    "email": "admin@mycompany.com"
}'

curl -XPUT http://localhost:9200/test/email/3?parent=1 -d '{
    "email": "abcd@efg.com"
}'

curl -XPUT http://localhost:9200/test/email/4?parent=2 -d '{
    "email": "peter@peter.com"
}'

curl -XPUT http://localhost:9200/test/email/5?parent=3 -d '{
    "email": "andy@yahoo.com"
}'

curl -XPUT http://localhost:9200/test/email/6?parent=3 -d '{
    "email": "support@mycompany.com"
}'

imotov’s solution worked for me. Another solution I have found is to query accounts for status = active, then run a bool filter on the result and use has_child on the child type and prefix on name inside the bool filter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:46:31+00:00Added an answer on June 9, 2026 at 10:46 am

    An important difference between elasticsearch and relational databases is that elasticsearch cannot perform joins. In elasticsearch you are always searching a single index or union of indices. But in case of parent/child relationship, it’s possible to limit results in the parent index using a query on the child index. For example, you can execute this query on the account type.

    {
        "bool": {
            "must": [
                { 
                    "text" : { "name": "foo" } 
                }, { 
                    "term" : { "state": "active" } 
                }, {
                    "has_child": {
                        "type": "email",
                        "query": {
                            "text": {"email": "bar" }
                        }
                    }
                }
            ]
        }
    }
    

    This query will return you the parent document only (no child documents will be returned). You can use the parent id returned by this query to find all children of this parent using the field _parent, which is stored and indexed by default.

    {
        "term" : { "_parent": "1" } 
    }
    

    Or you can limit your results only to the children that contain the word bar in the field email:

    {
        "bool": {
            "must": [
                { 
                    "term" : { "_parent": "1" } 
                }, { 
                    "text" : { "email": "bar" } 
                }
            ]
        }
    }
    

    I don’t think it’s possible to specify parent in the json unless you are using _bulk indexing.

    This is how email lookup can be implemented using test data provided in the question:

    #!/bin/sh
    curl -XDELETE 'http://localhost:9200/test' && echo 
    curl -XPOST 'http://localhost:9200/test' -d '{
        "settings" : {
            "number_of_shards" : 1,
            "number_of_replicas" : 0
        },
        "mappings" : {
          "account" : {
            "_source" : { "enabled" : true },
            "properties" : {
              "name": { "type": "string", "analyzer": "standard" },
              "statuses": { "type": "string",  "index": "not_analyzed" }
            }
          },
          "email" : {
            "_parent" : {
              "type" : "account"
            },
            "properties" : {
              "email": { "type": "string",  "analyzer": "standard" }
            }
          }
        }
    }' && echo
    
    curl -XPUT 'http://localhost:9200/test/account/1' -d '{
        "name": "John Smith",
        "statuses": "active"
    }'
    
    curl -XPUT 'http://localhost:9200/test/account/2' -d '{
        "name": "Peter Smith",
        "statuses": "active"
    }'
    
    curl -XPUT 'http://localhost:9200/test/account/3' -d '{
        "name": "Andy Smith",
        "statuses": "active"
    }'
    
    //Set up mapping for parent/child relationship
    
    curl -XPUT 'http://localhost:9200/test/email/1?parent=1' -d '{
        "email": "john@smith.com"
    }'
    
    curl -XPUT 'http://localhost:9200/test/email/2?parent=1' -d '{
        "email": "admin@mycompany.com"
    }'
    
    curl -XPUT 'http://localhost:9200/test/email/3?parent=1' -d '{
        "email": "abcd@efg.com"
    }'
    
    curl -XPUT 'http://localhost:9200/test/email/4?parent=2' -d '{
        "email": "peter@peter.com"
    }'
    
    curl -XPUT 'http://localhost:9200/test/email/5?parent=3' -d '{
        "email": "andy@yahoo.com"
    }'
    
    curl -XPUT 'http://localhost:9200/test/email/6?parent=3' -d '{
        "email": "support@mycompany.com"
    }'
    
    curl -XPOST 'http://localhost:9200/test/_refresh'
    echo
    curl 'http://localhost:9200/test/account/_search' -d '{
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "statuses": "active"
              }
            }
          ],
          "should": [
            {
              "prefix": {
                "name": "a"
              }
            },
            {
              "has_child": {
                "type": "email",
                "query": {
                  "prefix": {
                    "email": "a"
                  }
                }
              }
            }
          ],
          "minimum_number_should_match" : 1
        }
      }
    }' && echo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
I have been reading the docs and playing with different EventQuery parameters for days
After reading this: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it I came to the conclusion that it is not valid
I have been reading about making ajax heavy applications more search engine friendly: https://developers.google.com/webmasters/ajax-crawling/docs/getting-started
reading the docs i see that the glGetTexImage2d() function has a 'type' parameter. The
I am new to rails but was reading the docs about verifying params in
I am new to Spring and I was just reading the docs about Hibernate-Spring
After reading the docs,I'm doing it this way: ds /c 21 1de2458 But only
I'm having problems with a NonUniqueObjectException thrown by Hibernate. Reading the docs, and this
Been reading the docs about canvas and secure canvas, mainly due to the requirement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.