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

Categories

In my Elasticsearch index, I have a book type that has many pages. Using nested documents, I have successfully mapped the relationship and I can index documents like this:

PUT /my_index/book/1

{
  "title": "Harry Potter and the Chamber of Secrets",
  "pages": [
    {
      "id": 1,
      "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam a metus est. Duis ut est et mi feugiat bibendum feugiat eu tortor. Pellentesque accumsan, eros nec commodo euismod, odio dolor lobortis diam, in pulvinar lacus turpis sed justo. Ut placerat ut nulla sed blandit. Aenean vel turpis erat. Phasellus vehicula laoreet ex, nec dapibus leo tempus vitae. Nulla gravida efficitur metus, in euismod justo placerat sit amet. Maecenas tristique est mauris, sagittis scelerisque turpis suscipit vel. Nullam ultricies sapien sit amet neque aliquam hendrerit sed non nibh."
    },
    {
      "id": 2,
      "text": "Pellentesque facilisis turpis in diam maximus luctus. Mauris leo diam, pellentesque a malesuada vitae, scelerisque at ipsum. Fusce tincidunt neque dui. Nullam ac ex luctus, convallis leo eget, feugiat augue. Cras condimentum, purus eu scelerisque sodales, diam est commodo lectus, at finibus orci turpis nec lectus. Mauris in lectus ut diam finibus pellentesque quis tincidunt urna. Curabitur tristique luctus metus at interdum. Curabitur imperdiet ex vel enim pretium, a convallis velit tempor. Nullam odio eros, tincidunt ut consectetur non, scelerisque eget urna. Fusce placerat dui et odio tempus rutrum. Integer non dui eu ante interdum volutpat. Mauris quis ante sed lacus euismod mattis."
    }
  ]
}

I want to query and obtain all pages from a book which contain a certain word in the text field. I can do this like this:

POST /my_index/book/_search

{
    "_source": false,
    "query": {
        "nested": {
            "path": "pages",
            "query": {
                "query_string": {
                    "query": "Mundo",
                    "fields": ["pages.text"]
                }
            },
            "inner_hits": {}
        }
    }
}

My problem however, is that I cannot find a way to specify which book I want to search in. Since I already know which book I'll be searching all pages from. I was trying to do this with a ids query, but I cannot find a way to use it with the nested query together.

How can I specify the book I want to search in (pages from this book), and make the response return a list of all the id values of each page? This id value is a reference to the page's page number in the book.

See Question&Answers more detail:os

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

1 Answer

You can combine two conditions under Bool query like so:

{
  "_source": false,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "title": "Harry Potter and the Chamber of Secrets"
          }
        },
        {
          "nested": {
            "path": "pages",
            "query": {
              "query_string": {
                "query": "Mundo",
                "fields": [
                  "pages.text"
                ]
              }
            },
            "inner_hits": {
              "_source": [
                "pages.id"
              ]
            }
          }
        }
      ]
    }
  }
}

As can be seen, I have encapsulate your nested query under bool-must (AND condition) and added term which defines the title to filter by.

Second thing, as you described you want only the pages-ids. So, under inner_hits I added "_source": ["id"] which gives you the ids only (it just the same trick you did when you set the source off at the head of the query.

Pay attention, that, default size for inner_hits is 10, so you may also want to add "size": 1000 under inner_hits.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...