问题描述:

I guess I need some help with ElasticSearch.

I have a parent child mapping and I would like to search for documents containing the word in either the parent or in the child document. Think of a SO question and the answers. There is 1:n questions to answers. I would like to get the questions where the SeachWord is in the question (parent) and I would also like to get the question if the SearchWord is in any child answer even if the SearchWord is not in the question.

This is my query and the result is the child document but I was expecting to get the parent document.

{

"query": {

"bool": {

"should": [{

"query_string": {

"query": "SearchWord"

}

},

{

"has_child": {

"type": "answers",

"query": {

"match": {

"_all": "SearchWord"

}

}

}

}]

}

},

"size": 1

}

Thanks for your help!

UPDATE

The query is totally correct my only mistake was to run the query against the index.

Worng: cUrl /index/_search

Correct: cUrl /index/parenttype/_search

I hope this helps anyone who has problems with parent child searches ;-)

Regards

Phillip

网友答案:

You should query child and filter on parent

cUrl /indexName/childType

{
  "query": {
    "bool": {
      "should": [
        {
          "has_parent": {
            "type": "questions",
            "query": {
              "query_string": {
                "query": "SearchWord"
              }
            }
          }
        },
        {
          "match": {
            "_all": "SearchWord"
          }
        }
      ]
    }
  },
  "size": 1
}
网友答案:

your answer is not exactly what I was looking for but i gave my the right direction.
I have to query the parent type instead of the whole index. So this is my solution which is working like expected.

{
  "query": {
    "bool": {
      "should": [
        {
          "has_child": {
            "type": "answers",
            "query": {
              "query_string": {
                "query": "SearchWord"
              }
            }
          }
        },
        { 
            "query_string" : {
                "query": "SearchWord" 
            } 
        }
      ]
    }
  },
  "size": 1
}

Thank you very much!

相关阅读:
Top