# Query results non understood

**URL:** <https://discuss.elastic.co/t/query-results-non-understood/128364>\
**Category:** Elasticsearch\
**Created:** [April 17, 2018, 1:42pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364 "2018-04-17T13:42:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessie](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jessie](https://discuss.elastic.co/u/jessie)\
**Post date:** [April 17, 2018, 1:42pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364/1 "2018-04-17T13:42:14Z")

</div>

Hi everybody !

I have two indexes with the following mapping :

```
"skills_metiers": {
                "type": "nested",
                "properties": {
                    "metier": {
                        "type": "keyword"
                    },
                    "experience": {
                        "type": "keyword"
                    }
                }
            }

```

I want to search skills\_metiers from index 1 to index 2 :

```
def build_query(skills_metiers = None):

query = {
    "size": 30,
    "query": {
        "bool": {
            "should": [],
            "must": []
        }
    }
}

if skills_metiers:
    for metier in skills_metiers:
        query["query"]["bool"]["should"].append({
            "nested": {
                "path": "skills_metiers",
                "score_mode": "avg",
                "query": {
                    "bool": {
                        "must": {
                            "query_string": {
                                "query": "skills_metiers.metier:" + metier['metier'] + " AND skills_metiers.experience:" + metier['experience']
                            }
                        }
                    }
                }                    
            }
        })

return query

```

I search :  
[{'experience': 'DEBUTANT', 'metier': 'CP'},  
{'experience': 'DEBUTANT', 'metier': 'BI'},  
{'experience': 'DEBUTANT', 'metier': 'BA'}]

and the results are :

6.7111297  
[{'experience': 'DEBUTANT', 'metier': 'CP'},  
{'experience': 'DEBUTANT', 'metier': 'BA'},  
{'experience': 'DEBUTANT', 'metier': 'BI'}]

6.7111297  
[{'experience': 'DEBUTANT', 'metier': 'CP'},  
{'experience': 'DEBUTANT', 'metier': 'BA'},  
{'experience': 'DEBUTANT', 'metier': 'BI'}]

4.9342995  
[{'experience': 'DEBUTANT', 'metier': 'BA'},  
{'experience': 'DEBUTANT', 'metier': 'BI'}]

3.7447155  
[{'experience': 'DEBUTANT', 'metier': 'BA'},  
{'experience': 'DEBUTANT', 'metier': 'CP'}]

1.9678854  
[{'experience': 'DEBUTANT', 'metier': 'BA'}]

1.7768301  
[{'experience': 'DEBUTANT', 'metier': 'CP'}]

1.7768301  
[{'experience': 'DEBUTANT', 'metier': 'AMOA'},  
{'experience': 'DEBUTANT', 'metier': 'CP'}]

I don't understand why neither results 3 and 4 are not equals nor results 5, 6 and 7...

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 17, 2018, 1:55pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364/2 "2018-04-17T13:55:53Z")

</div>

I guess you are speaking about the score here?

May be you have 5 shards (default value)? Which will explain that.  
Go either with one shard or use query then fetch option.

---

<div class="post-metadata">

**Author:** ![jessie](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jessie](https://discuss.elastic.co/u/jessie)\
**Post date:** [April 17, 2018, 2:00pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364/3 "2018-04-17T14:00:17Z")

</div>

Yes I am speaking about score.

I have only 1 shard and 0 replica (for each index)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 17, 2018, 2:13pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364/4 "2018-04-17T14:13:25Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem.

---

<div class="post-metadata">

**Author:** ![jessie](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jessie](https://discuss.elastic.co/u/jessie)\
**Post date:** [April 17, 2018, 2:49pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364/5 "2018-04-17T14:49:14Z")

</div>

Here is my python script I use to do my queries.

```
    def build_query(skills_metiers = None):
    
    query = {
        "size": 30,
        "query": {
            "bool": {
                "should": [],
                "must": []
            }
        }
    }

    if skills_metiers:
        for metier in skills_metiers:
            query["query"]["bool"]["should"].append({
                "nested": {
                    "path": "skills_metiers",
                    "score_mode": "avg",
                    "query": {
                        "bool": {
                            "must": {
                                "query_string": {
                                    "query": "(skills_metiers.metier:" + metier['metier'] + ") AND (skills_metiers.experience:" + metier['experience'] + ")"
                                }
                            }
                        }
                    }                    
                }
            })

    
    return query

def search(es, document):
    
    try:    
        query = build_query(document['skills_metiers'])
        response = es.search(index = "index2", body = json.dumps(query))

        best_results = []
        for i in range(len(response['hits']['hits'])):
            best_results.append([response['hits']['hits'][i]['_source'],response['hits']['hits'][i]['_score']])
    
        return best_results
    
    except:
        pass

# Save results in a dataframe
documents = es.search(index = "index1", 
                    body = {
                        "size" : es.count(index = 'index1')["count"], 
                        "query": { "match_all": {} } 
                    })

rows = []
columns = ['id_index1', 'id_index2', 'score']
for document in documents['hits']['hits']:
    best_results = search(es, document['_source'])
    try:
        for result in best_results:
            row = [document['_source']['id'], result[0]['id'], result[1]]
            rows.append(row)
    except:
        pass

df_query = pd.DataFrame(rows, columns = columns)

```

My dataframe looks like that :

id\_index1 | id\_index2 | score  
91196 | 90082 | 6.7111297  
91196 | 90083 | 6.7111297  
... | ... | ...  
91196 | 90074 | 1.7768301

Then I write the field skills\_metiers for :

1. Document corresponding to id\_index1 = 91196 :

[{'experience': 'DEBUTANT', 'metier': 'CP'},  
{'experience': 'DEBUTANT', 'metier': 'BI'},  
{'experience': 'DEBUTANT', 'metier': 'BA'}]

1. Documents corresponding to id\_index2

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 17, 2018, 3:01pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364/6 "2018-04-17T15:01:22Z")

</div>

Yeah but I can't reproduce anything with that script. So please provide a pure REST script that anyone can copy and paste in Kibana console to reproduce your problem.

---

<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:** [May 15, 2018, 3:01pm UTC](https://discuss.elastic.co/t/query-results-non-understood/128364/7 "2018-05-15T15:01:52Z")

</div>

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