0

Given the following index result:

curl 'localhost:9200/index123/type123/_search?q=*&pretty'
...
            "_source": {
                "objectId": "objectId123",
                "list": [{
                    "name": "SomeName123",
                    "value": "SomeValue123",
                    "type": "SomeType123"},
                    {"name": "SomeOtherName123", ...}],
                "someOther": {
                    "i": "i",
                    "value": 3
                }
            }
...

Now I want to do a search and get all entries matching two fields, e.g. list.value=SomeValue123 && list.type=SomeType123. The problem is, that the result may be huge, so scrolling should be possible.

What I have so far:

SearchResponse scrollResp = elasticSearchClient
                        .prepareSearch("index123")
                        .setTypes("type123")
                        .setScroll(new TimeValue(60000))
                    .setQuery(
                            QueryBuilders.nestedQuery(
                                    "list",
                                    QueryBuilders
                                            .boolQuery()
                                            .must(QueryBuilders.matchQuery("value", "SomeValue123"))
                                            .must(QueryBuilders.matchQuery("type", "SomeType123")))

                    )
                    .setSize(100)
                    .execute()
                    .actionGet(); 

            SomeQueue<SomeBean> resultQ= new SomeQueue<SomeBean>();
            // Scroll until no hits are returned
            while (true) {

                resultQ.offer(getObjectOutOfHits(scrollResp.getHits().getHits()));

                scrollResp = elasticSearchClient
                        .prepareSearchScroll(scrollResp.getScrollId())
                        .setScroll(new TimeValue(60000))
                        .execute()
                        .actionGet();
                // Break condition: No hits are returned
                if (scrollResp.getHits().getHits().length == 0) {
                    break;
                }
            }

But all I get is a:

Caused by: org.elasticsearch.index.query.QueryParsingException: [nested] failed to find nested object under path [list]

How can I get all items, that match this fields combination with the elastic search java client?

If someone knows just the curl command, that would be fine, so I can use templates!

4
  • Little bit offtopic, but you could use do...while() loop insteed of breaking out ;) Commented May 31, 2016 at 21:59
  • You are right, but I just copied this code from scroll example from elastic.co/guide/en/elasticsearch/client/java-api/current/… Commented Jun 1, 2016 at 7:56
  • For nested query to work, you need to have nested mapping, which you don't have probably. Can add the mapping perhaps? Without that the above query can't work. Commented Aug 25, 2016 at 15:06
  • Can you please add an example as answer? Commented Aug 26, 2016 at 13:28

1 Answer 1

1

Why are you nesting query into empty query? Try this:

    .setQuery(
        QueryBuilders.boolQuery()
            .must(QueryBuilders.matchQuery("value", "SomeValue123"))
            .must(QueryBuilders.matchQuery("type", "SomeType123"))
        )
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this and I get no hits, but there are some matching entries in elastic search.
I am working with elasticsearch right now and i ran coupe times into similar effects - no hits while there should be some. This were usually caused by misstyped field names. ES will not throw error if you try to query on not existing field for example. Also match query may be not returning hits due to its nature (it is not the equivalent of ==, term query is). Please use JSON query that in your opinion should return the correct answer ans add it to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.