Sorting on nested elements


(Blake McBride) #1

Greetings,

I have a query that I want to be sorted by specific nested elements. I get back the correct result with no errors, but it is not sorted. I feel confident that it is something to do with the "sort" element of my query. Really kind of lost. Sure appreciate some help.

Mapping:

{
  "components": {
    mappings: {
      component: {
        properties: {
          classifications: { type: "string", index: "not_analyzed" },
          componentProperties: {
            type: "nested",
            properties: {
              name: { type: "string" },
              numericValue: { type: "double" },
              stringValue: { type: "string" },
              units: { type: "string" }
            }
          },
          name: { type: "string" },
          position: { type: "long" }
        }
      }
    }
  }
}

Query:

curl -s -XGET 'http://localhost:9200/components/_search' -d '{
  from: 0,
  size: 20,
  query: {
    bool: {
      must: [
        {
          match_phrase_prefix: { classifications: "passive_components/resistors/single_components/" }
        }
      ]
    }
  },
  sort: [
    {
      "componentProperties.numericValue": {
        order: "asc",
        nested_filter: { term: { "componentProperties.name": "Resistance" } }
      }
    }
  ]
}
'

So, I want it sorted by the numericValue element when that element is associated with name=Resistance.

Example single document:

  {
    _index: "components",
    _type: "component",
    _id: "391a635c-ee56-46d1-9d9e-343d89b323c5",
    _score: null,
    _source: {
      name: "R 1206 1.1 0.333W 5% Thick Film",
      classifications: "passive_components/resistors/single_components/",
      componentProperties: [
        {
          name: "Resistance",
          stringValue: "1.1",
          numericValue: 1.1,
          units: "ohm"
        },
        {
          name: "Cp",
          stringValue: "0.07e-12",
          numericValue: 7e-14,
          units: "F"
        },
        { name: "Octopart mpn", stringValue: "ESR18EZPJ1R1" },
        {
          name: "Ls",
          stringValue: "0.95e-9",
          numericValue: 9.5e-10,
          units: "H"
        }
      ],
      position: 12
    },
    sort: ["Infinity"]
  }

Sure appreciate some help!

Thanks.

Blake McBride


(Adrien Grand) #2

I suspect the issue is with your nested filter: you are using a term query on an analyzed field, so you are trying to find "Resistance" while the index probably contains "resistance" (lowercase). You should either make your componentProperties.name field not_analyzed (typically a good idea if it is an enum) or use a match query instead of a term query (typically a better idea if this is a full text field).


(Blake McBride) #3

Downcasing 'Resistance' fixed the problem. It would be great if auto-downcasing indexes was highlighted in the docs when discussing sorting and elsewhere where it is applicable.

I really appreciate your help. I don't know how I would have figured it out without your help.

Thanks.

Blake


(Blake McBride) #4

Interestingly, when you do a nested search, the data elements are not downcased, but when you do a nested sort, they are. Seems inconsistent.


(Blake McBride) #5

It is now working for the "resistance" field, but it doesn't work with the "octopart mpn" field. I can't figure out why not. Could it be because of the space in the name?

Thanks!

Blake


(Adrien Grand) #6

Sorry I'm not following what you mean here, could you elaborate a bit (an example would be great)?


(system) #7