2

I'm having weird results on some nested objects that I can't understand. I mean, in mapping the nested objects are all the same, they have an id and a tree id also, both are long and thats it, but profession and category do not return the expected values, but the others do (professionType, professionSubtype and attributeType).

So I got index mapped as:

Mapping:

{
  "attribute-tree": {
    "mappings": {
      "attribute": {
        "dynamic": "strict",
        "properties": {
          "attributeType": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "category": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "family": {"type": "long"},
          "id": {"type": "long"},
          "name": {
            "type": "string",
            "index": "not_analyzed"
          },
          "parentTree": {"type": "long"},
          "profession": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "professionSubtype": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "professionType": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "sorter": {
            "properties": {
              "id": {"type": "long"},
              "name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "tree": {"type": "long"}
            }
          },
          "suggester": {
            "type": "completion",
            "index_analyzer": "edgeNGram_analyzer",
            "search_analyzer": "whitespace_analyzer",
            "payloads": true,
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50
          },
          "tree": {"type": "long"},
          "type": {"type": "string"}
        }
      },
      "division": {
        // same as "attribute"
      },
      "profession-subtype": {
        // same as "attribute"
      },
      "profession-type": {
        // same as "attribute"
      },
      "profession": {
        // same as "attribute"
      },
      "category": {
        // same as "attribute"
      }
    }
  }
}

If any kind of information is missing, please say

Thanks in advance

Example

Filtering by category.id:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"category.id": 1}
      }
    }
  }
}

I get:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 31,
      "max_score": 1,
      "hits": [
         {
            "_index": "attribute-tree",
            "_type": "category",
            "_id": "4064",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4064,
               "name": "Profession",
               "type": "C",
               "parentTree": 4063,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 6,
                  "tree": 4063
               },
               "category": {
                  "id": null,
                  "tree": null
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  4063,
                  1
               ],
               "suggester": {
                  "input": [
                     "Profession"
                  ],
                  "output": "Profession"
               },
               "sorter": {
                  "name": "Profession",
                  "id": 1,
                  "tree": 4064
               }
            }
         },
         {
            "_index": "attribute-tree",
            "_type": "category",
            "_id": "4083",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4083,
               "name": "Profession",
               "type": "C",
               "parentTree": 4082,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 2,
                  "tree": 4072
               },
               "professionSubtype": {
                  "id": 8,
                  "tree": 4082
               },
               "category": {
                  "id": null,
                  "tree": null
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  4082,
                  4072
               ],
               "suggester": {
                  "input": [
                     "Profession"
                  ],
                  "output": "Profession"
               },
               "sorter": {
                  "name": "Profession",
                  "id": 1,
                  "tree": 4083
               }
            }
         }
      ]
   }
}

Notice that on this example the category.id doesn't have the expected value (this is also valid for when it is 0 instead of null)

However I have nodes with category.id = 1:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"tree": 4}
      }
    }
  }
}

Item:

{
   "took": 5,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "attribute-tree",
            "_type": "profession",
            "_id": "4",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4,
               "name": "A&R Administrator",
               "type": "P",
               "parentTree": 3,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 1,
                  "tree": 2
               },
               "category": {
                  "id": 1,
                  "tree": 3
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  3,
                  2,
                  1
               ],
               "suggester": {
                  "input": [
                     "A&R",
                     "Administrator"
                  ],
                  "output": "A&R Administrator"
               },
               "sorter": {
                  "name": "A&R Administrator",
                  "id": 1,
                  "tree": 4
               }
            }
         }
      ]
   }
}

Filtering by professionType.id:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"professionSubtype.id": 1}
      }
    }
  }
}

I get:

{
   "took": 7,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3890,
      "max_score": 1,
      "hits": [
         {
            "_index": "attribute-tree",
            "_type": "category",
            "_id": "251",
            "_score": 1,
            "_source": {
               "id": 4,
               "tree": 251,
               "name": "Medium",
               "type": "C",
               "parentTree": 2,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 1,
                  "tree": 2
               },
               "category": {
                  "id": null,
                  "tree": null
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  2,
                  1
               ],
               "suggester": {
                  "input": [
                     "Medium"
                  ],
                  "output": "Medium"
               },
               "sorter": {
                  "name": "Medium",
                  "id": 4,
                  "tree": 251
               }
            }
         },
         {
            "_index": "attribute-tree",
            "_type": "profession",
            "_id": "4",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4,
               "name": "A&R Administrator",
               "type": "P",
               "parentTree": 3,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 1,
                  "tree": 2
               },
               "category": {
                  "id": 1,
                  "tree": 3
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  3,
                  2,
                  1
               ],
               "suggester": {
                  "input": [
                     "A&R",
                     "Administrator"
                  ],
                  "output": "A&R Administrator"
               },
               "sorter": {
                  "name": "A&R Administrator",
                  "id": 1,
                  "tree": 4
               }
            }
         }
      ]
   }
}

Also tested as:

POST /attribute-tree/_search
{
  "query":{
    "filtered": {
      "query": {"match_all":{}},
      "filter": {
        "nested": {
          "path": "category",
          "filter": {
            "bool": {
              "must": [
                {"term": {"category.id": 1}}
              ]
            }
          }
        }
      }
    }
  }
}

But this gives an error

org.elasticsearch.index.query.QueryParsingException: [attribute-tree] [nested] nested object under path [category] is not of nested type
1
  • as per the mapping category is object type and not nested. If you need to use the nested filter it should be of type nested . Commented Sep 15, 2015 at 14:21

1 Answer 1

1

From the mapping it looks like the index has a type of category as well as a field in attribute type called category.

To allow for proper field resolution and disambiguate between field id in type category vs field category.id in type attribute you would need to specify the entire path to the field including the type i.e <type>.<fieldname>.

Example:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"attribute.category.id": 1}
      }
    }
  }
}

This issue thread has more discussion with regard to this.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually that worked, but still got the problem, because i intend to search on all core's. I've tried with *.category.id but it didn't work, do you have a suggestion ? thanks
a tedious workaround should clause with multiple terms filter for category.id on each type ,
another option is to restructure the index since all types have same "schema" create a single type .The document should contain document-type field along with current data.
When you mean single type, you're talking in merging the attributes, categories, professions, etc... into one, is that correct?
Actually I was thinking in what arm can that do, but I can split them by the values in the objects, so I'm going to give it a try.

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.