Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:48:20+00:00 2026-06-17T20:48:20+00:00

I am facing a trouble in the use of ElasticSearch for my java application.

  • 0

I am facing a trouble in the use of ElasticSearch for my java application.
I explain myself, I have a mapping, which is something like :

{
"products": {
    "properties": {
        "id": {
            "type": "long",
                   "ignore_malformed": false
        },
        "locations": {
            "properties": {
                "category": {
                    "type": "long",
                   "ignore_malformed": false
                },
                "subCategory": {
                    "type": "long",
                   "ignore_malformed": false
                },
                "order": {
                    "type": "long",
                   "ignore_malformed": false
                }
            }
        },
...

So, as you can see, I receive a list of products, which are composed of locations. In my model, this locations are all the categories’ product. It means that a product can be in 1 or more categories. In each of this category, the product has an order, which is the order the client wants to show them.

For instance, a diamond product can have a first place in Jewelry, but the third place in Woman (my examples are not so logic ^^).
So, when I click on Jewelry, I want to show this products, ordered by the field locations.order in this specific category.

For the moment, when I search all the products on a specific category the response for ElasticSearch that I receive is something like :

{"id":5331880,"locations":[{"category":5322606,"order":1},
{"category":5883712,"subCategory":null,"order":3},
{"category":5322605,"subCategory":6032961,"order":2},.......

Is it possible to sort this products, by the element locations.order for the specific category I am searching for ? For instance, if I am querying the category 5322606, I want the order 1 for this product to be taken.

Thank you very much beforehand !
Regards,
Olivier.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:48:22+00:00Added an answer on June 17, 2026 at 8:48 pm

    First a correction of terminology: in Elasticsearch, “parent/child” refers to completely separate docs, where the child doc points to the parent doc. Parent and children are stored on the same shard, but they can be updated independently.

    With your example above, what you are trying to achieve can be done with nested docs.

    Currently, your locations field is of type:"object". This means that the values in each location get flattened to look something like this:

    { 
        "locations.category": [5322606, 5883712, 5322605],
        "locations.subCategory": [6032961],
        "locations.order": [1, 3, 2]
    }
    

    In other words, the “sub” fields get flattened into multi-value fields, which is of no use to you, because there is no correlation between category: 5322606 and order: 1.

    However, if you change locations to be type:"nested" then internally it will index each location as a separate doc, meaning that each location can be queried independently, using the dedicated nested query and filter.

    By default, the nested query will return a _score based upon how well each location matches, but in your case you want to return the highest value of the order field from any matching children. To do this, you’ll need to use a custom_score query.

    So let’s start by creating the index with the appropriate mapping:

    curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'  -d '
    {
       "mappings" : {
          "products" : {
             "properties" : {
                "locations" : {
                   "type" : "nested",
                   "properties" : {
                      "order" : {
                         "type" : "long"
                      },
                      "subCategory" : {
                         "type" : "long"
                      },
                      "category" : {
                         "type" : "long"
                      }
                   }
                },
                "id" : {
                   "type" : "long"
                }
             }
          }
       }
    }
    '
    

    The we index your example doc:

    curl -XPOST 'http://127.0.0.1:9200/test/products?pretty=1'  -d '
    {
       "locations" : [
          {
             "order" : 1,
             "category" : 5322606
          },
          {
             "order" : 3,
             "subCategory" : null,
             "category" : 5883712
          },
          {
             "order" : 2,
             "subCategory" : 6032961,
             "category" : 5322605
          }
       ],
       "id" : 5331880
    }
    '
    

    And now we can search for it using the queries we discussed above:

    curl -XGET 'http://127.0.0.1:9200/test/products/_search?pretty=1'  -d '
    {
       "query" : {
          "nested" : {
             "query" : {
                "custom_score" : {
                   "script" : "doc[\u0027locations.order\u0027].value",
                   "query" : {
                      "constant_score" : {
                         "filter" : {
                            "and" : [
                               {
                                  "term" : {
                                     "category" : 5322605
                                  }
                               },
                               {
                                  "term" : {
                                     "subCategory" : 6032961
                                  }
                               }
                            ]
                         }
                      }
                   }
                }
             },
             "score_mode" : "max",
             "path" : "locations"
          }
       }
    }
    '
    

    Note: the single quotes within the script have been escaped as \u0027 to get around shell quoting. The script actually looks like this: "doc['locations.order'].value"

    If you look at the _score from the results, you can see that it has used the order value from the matching location:

    {
       "hits" : {
          "hits" : [
             {
                "_source" : {
                   "locations" : [
                      {
                         "order" : 1,
                         "category" : 5322606
                      },
                      {
                         "order" : 3,
                         "subCategory" : null,
                         "category" : 5883712
                      },
                      {
                         "order" : 2,
                         "subCategory" : 6032961,
                         "category" : 5322605
                      }
                   ],
                   "id" : 5331880
                },
                "_score" : 2,
                "_index" : "test",
                "_id" : "cXTFUHlGTKi0hKAgUJFcBw",
                "_type" : "products"
             }
          ],
          "max_score" : 2,
          "total" : 1
       },
       "timed_out" : false,
       "_shards" : {
          "failed" : 0,
          "successful" : 5,
          "total" : 5
       },
       "took" : 9
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a trouble with my application: I have a constraint on my database
I am just facing trouble to sort out the query I need. I have
I'm facing some trouble when using the class QNetworkAccessManager . I use it to
If I use only CAMERA_FACING_BACK or CAMERA_FACING_FRONT all works fine. I have trouble with
Im facing a problem where i want to schedule a certain java application to
We are facing trouble restarting closing a running *.JAR file started with the java
Good afternoon I'm facing a trouble, trying to figure out how to use this
I am facing trouble to develop a RMI application in Netbeans 7.0. Is there
I am facing trouble with the valueChangeListener of h:selectBooleanCheckbox . I have the following
While developing an application (for KIOSK) using Actionscript I am facing the trouble to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.