Elastic Search

PROBLEM STATEMENT

I am using Join field type

Sample Mapping

 {
  "mappings": {
    "properties": {
      "firstname": {
        "type": "keyword"
      },
"lastname": {
        "type": "keyword"
      },
      "my_join_field": { 
        "type": "join",
        "relations": {
          "question": "answer" 
        }
      }
    }
  }
}

I have a scenario where i want to search based on first name and include all documents (question and answers)
Also if a question is in the search result i want to include its answers also.
Result set should have unique values

My Search Query is

{
    "from": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "prefix": {
                        "firstName": "J"
                    }
                }
            ]
        }
    },
    "size": 2,
    "sort": [
        {
            "lastName": {
                "order": "desc"
            }
        },
        {
            "firstName": {
                "order": "asc"
            }
        }
          ]
}

Problem: How to get exact matched (including child) hit count.
The from and size does not work for child, also sort is not working

Sample Data:-

curl --location 'members/_doc/1' \
--header 'Content-Type: application/json' \
--data '{
  
   "firstname": "John",
  "lastname": "Doe",
  "relationship": {
    "name": "question"
  }
}'


curl -X POST "members/_doc/2?routing=1"
{
  "firstname": "Jane",
  "lastname": "Doe",
  "relationship": {
    "name": "answer",
    "parent": "1" 
  }
}


curl -X POST "members/_doc/3?routing=1"
{
  "firstname": "Joe",
  "lastname": "XYZ",
  "relationship": {
    "name": "answer",
    "parent": "1" 
  }
}

curl -X POST members/_doc/4?routing=1"
{
  "firstname": "Ken",
  "lastname": "Blanton",
  "relationship": {
    "name": "answer",
    "parent": "1" 
  }
}

Any Update on this?

Welcome!

You can look at

and

That should help.

But depending on your use case, it might be worth to index the question and all the answers in a single document...

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.