Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

elasticsearch - Inner hits not working with nested filter?

I've just upgraded to Elastic Search 1.5.0 and so far I can't make inner_hits work with a nested filter, although it works fine with a nested query.

Let's say I want to retrieve the inner nested object actors within a movie object.

When I run the following nested query :

Syntax 1

GET my_index/movie/_search
{
  "query": {
    "filtered": {
      "query": {"match_all": {}},
      "filter": {
        "nested": {
          "path": "actors",
          "query": {
            "match": {
              "actors.id": 12345
            }
          }, 
          "inner_hits" : {}
        }
      }
    }
  }
}

=> I get the inner_hits as documented here, which is just fine.

But when I try doing the equivalent query with a nested filter :

Syntax 2

GET my_index/movie/_search
{
  "query": {
    "filtered": {
      "query": {"match_all": {}},
      "filter": {
        "nested": {
          "path": "actors",
          "filter": {
            "term": {
              "actors.id": 12345
            }
          }, 
          "inner_hits" : {}
        }
      }
    }
  }
}

=> I get the following parse error

QueryParsingException[[my_index] [nested] requires either 'query' or 'filter' field]

(and this last query works fine when I remove inner_hits - except of course that I don't get the inner hits ...)

Is there something wrong in the syntax I use or is the inner_hits not implemented yet with nested filter ?

Thanks in advance

Edit 3-30-2015

It works with the syntax provided below by @mdewit (thanks!)

Syntax 3

GET my_index/movie/_search
{
    "query": {
        "nested": {
            "path": "actors",
            "query": {
                "filtered": {
                    "filter": {
                        "term": {"actors.id": 12345}
                    }
                }
            },
            "inner_hits" : {}
        }
    }
}

even though this syntax does not match the Nested Filter doc

=> I still do not understand what is wrong with Syntax 2. It seems like an ES bug to me.

Edit 04-22-2015 : bug fixed in ES 1.5.1, see my comment below

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following seems to work:

GET my_index/movie/_search
{
    "query": {
        "nested": {
            "path": "actors",
            "query": {
                "filtered": {
                    "filter": {
                        "term": {"actors.id": 12345}
                    }
                }
            },
            "inner_hits" : {}
        }
    }
}'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...