# Filter and sort documents based on nested fields

**URL:** <https://discuss.elastic.co/t/filter-and-sort-documents-based-on-nested-fields/150859>\
**Category:** Elasticsearch\
**Created:** [October 3, 2018, 10:43am UTC](https://discuss.elastic.co/t/filter-and-sort-documents-based-on-nested-fields/150859 "2018-10-03T10:43:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![allan\_benguigui](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@allan\_benguigui](https://discuss.elastic.co/u/allan_benguigui)\
**Post date:** [October 3, 2018, 10:43am UTC](https://discuss.elastic.co/t/filter-and-sort-documents-based-on-nested-fields/150859/1 "2018-10-03T10:43:45Z")

</div>

Hi everyone,

I'm using Elasticsearch to search easily people based on theirs skills.  
I tried to find answer of my question in related post, but I did not.

Here is what a document looks likes:

```
{
      "_index" : "danny-index-node",
      "_type" : "user",
      "_id" : "c6ddfe51-c2fa-11e8-bdc5-fa163e1c0c40",
      "_score" : 3.757271,
      "_source" : {
        "lastName" : [],
        "domainsCity" : [],
        "domainsTwitter" : [],
        "githubJobs" : [],
        "stackBio" : [],
        "stackJobs" : [],
        "stackPicture" : [],
        "signaturePhones" : [],
        "domainSectors" : [],
        "uuid" : "XXX",
        "emails" : ["XXX@gmail.com"],
        "skills" : [ {
          "github" : 1.5,
          "name" : "CSS",
          "stackoverflow" : 0
        }, {
          "github" : 3.08,
          "name" : "HTML",
          "stackoverflow" : 0
        }, {
          "github" : 0.03,
          "name" : "PHP",
          "stackoverflow" : 0
        }, {
          "github" : 0.11,
          "name" : "TeX",
          "stackoverflow" : 0
        }, {
          "github" : 6.46,
          "name" : "R",
          "stackoverflow" : 0
        } ],
        "hasEmail" : 1,
        "githubPicture" : ["https://avatars1.githubusercontent.com/u/XXX"],
        "gravatar" : [""],
        "domainsName" : [],
        "id" : XXX,
        "signatureJobs" : [],
        "domainsCountry" : [],
        "domainsDescription" : [],
        "gmailName" : "john smith",
        "signatureAddresses" : [],
        "stackCompany" : [],
        "domainTags" : [],
        "linkedinLocation" : [],
        "jobs" : [],
        "githubBio" : ["synthetic biologist, plant engineer, reproducible research enthusiast"],
        "domains" : [],
        "linkedinSchool" : [],
        "linkedinCompany" : [],
        "domainsLinkedin" : [],
        "socialAccounts" : [ {
          "label" : ["SocialAccountGH"],
          "id" : "johnsmith"
        } ],
        "stackReach" : [],
        "stackLocation" : [],
        "firstName" : [],
        "domainIndustry" : [],
        "domainsCrunchbase" : [],
        "githubLocation" : ["Seattle"],
        "githubFollowers" : [4],
        "name" : ["john smith"],
        "linkedinJobs" : [],
        "linkedinPicture" : [],
        "typeConnexion" : "gmail",
        "githubCompany" : ["University of Washington"]
      }
    }

```

I'm trying to get People who have great skills in R AND Javascript, based on their github/stackoverflow value.

Here is my query :

```
{
    "size": 10,
    "from": 0,
    "query": {
        "filtered": {
            "query": {
                "nested": {
                    "path": "skills",
                    "query": {
                        "function_score": {
                            "query": {
                                "terms": {
                                    "skills.name": ["r","javascript"]
                                    
                                }
                            },
                            "functions": [
                                {
                                    "field_value_factor": {
                                        "field": "skills.github",
                                        "factor": 1.5 
                                    }
                                },
                                {
                                    "field_value_factor": {
                                        "field": "skills.stackoverflow",
                                        "factor": 1.2 
                                    }
                                }
                                ],
                            "boost_mode": "sum"
                        }
                    }
                }
            }
        }
    }
}

```

Unfortunately, I got wrong result, the query tries to find people with at least one of the skills R or javascript and not having both skills. And secondly the first document is not the one who have the better score in R or Javascript.

Can anyone explain me how to deal with this query?

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 31, 2018, 10:43am UTC](https://discuss.elastic.co/t/filter-and-sort-documents-based-on-nested-fields/150859/2 "2018-10-31T10:43:46Z")

</div>

Closed on October 31, 2018, 10:43am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
