1

I'm creating a query on Elasticsearch, for find documents through all indices.
I need to combine should, must and nested query on Elasticsearch, i get the right result but i get an error inside the result.

This is the query I'm using

GET _all/_search
{
  "query": {
      "bool": {
        "minimum_should_match": 1,
        "should": [
          { "term": { "trimmed_final_url": "https://www.repubblica.it/t.../" } }
        ],
        "must": [
          {
            "nested": {
              "path": "entities",
              "query": {
                "bool": {
                  "must": [
                    { "term": { "entities.id": "138511" } }
                  ]
                }
              }
            }
          },
          {
            "term": {
              "language": { "value": "it" }
             }
          }
        ]
      }
  }

And this is the result

{
  "_shards" : {
    "total" : 38,
    "successful" : 14,
    "skipped" : 0,
    "failed" : 24,
    "failures" : [
      {
        "shard" : 0,
        "index" : ".kibana_1",
        "node" : "7twsq85TSK60LkY0UiuWzA",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : """
            failed to create query: {
            ...
              "index_uuid" : "HoHi97QFSaSCp09iSKY1DQ",
              "index" : ".reporting-2019.06.02",
              "caused_by" : {
                "type" : "illegal_state_exception",
                "reason" : "[nested] failed to find nested object under path [entities]"
              }
            }
          },
            ...
  "hits" : {
    "total" : {
      "value" : 50,
      "relation" : "eq"
    },
    "max_score" : 16.90015,
    "hits" : [
      {
        "_index" : "i_201906_v1",
        "_type" : "_doc",
        "_id" : "MugcbmsBAzi8a0oJt96Q",
        "_score" : 16.90015,
        "_source" : {
          "language" : "it",
          "entities" : [
            {
              "id" : 101580,
            },
            {
              "id" : 156822,
            },
            ...

I didn't write some fields because the code is too long

1 Answer 1

5

I am new to StackOverFlow (made this account to answer this question :D) so if this answer is out of line bear with me. I have been dabbling in nested fields in Elasticsearch recently so I have some ideas as to how this error could be appearing. Have you defined a mapping for your document type? I don't believe Elasticsearch will recognize the field as nested if you do not tell it to do so in the mapping:

PUT INDEX_NAME
{
  "mappings": {
      "DOC_TYPE": {
          "properties": {
              "entities": {"type": "nested"}
          }
      }
   }
}

You may have to specify this mapping for each index and document type. Not sure if there is a way to do that all with one request.

I also noticed you have a "should" clause with minimum matches set to 1. I believe this is exactly the same as a "must" clause so I am not sure what purpose this achieves (correct me if I'm wrong). If your mapping is specified, the query should look something like this:

GET /_all/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "entities",
            "query": {
              "term": {
                "entities.id": {
                  "value": "138511"
                }
              }
            }
          }
        },
        {
          "term": {
            "language": {
              "value": "it"
            }
          }
        },
        {
          "term": {
            "trimmed_final_url": {
              "value": "https://www.repubblica.it/t.../"
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answer, I have a "should" clause with minimum matches set to 1, because i would test the speed. The mapping exist. I think there are some document doesn't match the mapping or there are some other index with document that have different mapping. When i'll found the issue i will answer. Sorry for my English and thank you again!

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.