# Recherche de valeur distincte d'un champs

**URL:** https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308
**Category:** Discussions en français
**Created:** [April 9, 2018, 11:02am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308 "2018-04-09T11:02:33Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 9, 2018, 11:02am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/1 "2018-04-09T11:02:34Z")

</div>

Bonjour,  
Je stocke dans un index ce genre de document.  
Un search par un champs me donne

```
curl -s -XGET 'localhost:9200/loadresult/result/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "term" : {
                    "externalId" : "88"
                }
            }
        }
    }
,"sort": { "timestamp": { "order": "desc" }}
}'

```

Reponse:  
{  
"took" : 5,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"skipped" : 0,  
"failed" : 0  
},  
"hits" : {  
"total" : 3,  
"max\_score" : null,  
"hits" : [  
{  
"\_index" : "loadresult",  
"\_type" : "result",  
"\_id" : "76929376-bbbb-44ba-b6bf-9f1ddc8445d2",  
"\_score" : null,  
"\_source" : {  
"serverInfo" : {  
"jettyVersion" : "9.4.8.v20171121",  
"availableProcessors" : 32,  
"totalMemory" : 1098907648,  
"gitHash" : "82b8fb23f757335bb3329d540ce37a2a2615f0a8",  
"javaVersion" : "1.8.0\_162"  
},  
"collectorInformations" : {  
"totalCount" : 112407,  
"minValue" : 153600,  
"maxValue" : 27901951,  
"value50" : 407551,  
"value90" : 467455,  
"mean" : 422808.0,  
"stdDeviation" : 258593.0,  
"startTimeStamp" : 1523187253297,  
"endTimeStamp" : 1523187503420  
},  
"loadConfigs" : [  
{  
"threads" : 8,  
"warmupIterationsPerThread" : 0,  
"iterationsPerThread" : 1,  
"runFor" : 240,  
"usersPerThread" : 1,  
"channelsPerUser" : 6,  
"resourceRate" : 500,  
"scheme" : "http",  
"host" : "10.0.0.20",  
"port" : 8080,  
"maxRequestsQueued" : 1024,  
"type" : "PROBE",  
"resourceNumber" : 0,  
"instanceNumber" : 0  
},  
{  
"threads" : 8,  
"warmupIterationsPerThread" : 0,  
"iterationsPerThread" : 1,  
"runFor" : 240,  
"usersPerThread" : 4,  
"channelsPerUser" : 10,  
"resourceRate" : 700,  
"scheme" : "http",  
"host" : "10.0.0.20",  
"port" : 8080,  
"maxRequestsQueued" : 50000,  
"type" : "LOADER",  
"resourceNumber" : 30,  
"instanceNumber" : 2  
}  
],  
"uuid" : "76929376-bbbb-44ba-b6bf-9f1ddc8445d2",  
"externalId" : "88",  
"comment" : null,  
"uuidPrefix" : null,  
"timestamp" : "2018-04-08T06:38.23-0500"  
},  
"sort" : [  
1523187493800  
]  
}  
]  
}  
}

Je cherche maintenant à connaître tous les valeurs possibles du champs jettyVersion.  
Je pensais pouvoir utiliser:

```
curl -s -XGET 'localhost:9200/loadresult/result/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "jettyVersion" : {
            "terms" : { "field" : "serverInfo.jettyVersion" }
        }
    }
}'

```

La reponse est

```
 {
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [serverInfo.jettyVersion] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "loadresult",
        "node" : "cDMu8hfQQF2L-WpMbCAqVg",
        "reason" : {
          "type" : "illegal_argument_exception",
          "reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [serverInfo.jettyVersion] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
        }
      }
    ]
  },
  "status" : 400
}

```

Donc je pensais pouvoir utiliser

```
curl -s -XPUT 'localhost:9200/loadresult/_mapping/_doc?pretty' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "serverInfo.jettyVersion": {
      "type": "text",
      "fielddata": true
    }
  }
}
'
response:
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Mapper for [serverInfo.jettyVersion] conflicts with existing mapping in other types:\n[mapper [serverInfo.jettyVersion] is used by multiple types. Set update_all_types to true to update [fielddata] across all types.]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Mapper for [serverInfo.jettyVersion] conflicts with existing mapping in other types:\n[mapper [serverInfo.jettyVersion] is used by multiple types. Set update_all_types to true to update [fielddata] across all types.]"
  },
  "status" : 400
}

curl -s -XPUT 'localhost:9200/loadresult/_mapping/_doc?pretty&update_all_types=true' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "serverInfo.jettyVersion": {
      "type": "text",
      "fielddata": true
    }
  }
}
'

response

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Rejecting mapping update to [loadresult] as the final mapping would have more than 1 type: [result, _doc]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Rejecting mapping update to [loadresult] as the final mapping would have more than 1 type: [result, _doc]"
  },
  "status" : 400
}

```

Si quelqu'un a une idée je suis preneur 🙂

---

<div class="post-metadata">

### Author: ![Melvyn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/melvyn/32/35269_2.png) [@Melvyn](https://discuss.elastic.co/u/Melvyn)
#### Post date: [April 9, 2018, 11:22am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/2 "2018-04-09T11:22:16Z")

</div>

Est-ce que vous avez activé fieldata dans le mapping de l'index?

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 9, 2018, 11:38am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/3 "2018-04-09T11:38:02Z")

</div>

J'ai bien essayé via

```
curl -s -XPUT 'localhost:9200/loadresult/_mapping/_doc?pretty' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "serverInfo.jettyVersion": {
      "type": "text",
      "fielddata": true
    }
  }
}
'
response:
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Mapper for [serverInfo.jettyVersion] conflicts with existing mapping in other types:\n[mapper [serverInfo.jettyVersion] is used by multiple types. Set update_all_types to true to update [fielddata] across all types.]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Mapper for [serverInfo.jettyVersion] conflicts with existing mapping in other types:\n[mapper [serverInfo.jettyVersion] is used by multiple types. Set update_all_types to true to update [fielddata] across all types.]"
  },
  "status" : 400
}

```

ou

```
curl -s -XPUT 'localhost:9200/loadresult/_mapping/_doc?pretty&update_all_types=true' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "serverInfo.jettyVersion": {
      "type": "text",
      "fielddata": true
    }
  }
}
'

response

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Rejecting mapping update to [loadresult] as the final mapping would have more than 1 type: [result, _doc]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Rejecting mapping update to [loadresult] as the final mapping would have more than 1 type: [result, _doc]"
  },
  "status" : 400
}

```

Pas la bonne requete?

---

<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 9, 2018, 2:30pm UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/4 "2018-04-09T14:30:04Z")

</div>

Salut Olivier

Tu ne peux pas changer un champ déjà indexé.

Ici en l'occurence au lieu d'activer fielddata, je changerai le type du champ `serverInfo.jettyVersion` en `keyword`.

A moins que tu ne veuilles faire aussi du fulltext search dessus auquel cas, je le mettrai en `text` et un sous-champ de type `keyword`:

```auto
DELETE test
PUT test
{
  "mappings": {
    "doc": {
      "properties": {
        "serverInfo.jettyVersion": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
PUT test/doc/1
{
  "serverInfo.jettyVersion": "1.2.3"
}
GET test/_search
{
  "size": 0,
  "aggs": {
    "version": {
      "terms": {
        "field": "serverInfo.jettyVersion.keyword"
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 10, 2018, 12:15am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/5 "2018-04-10T00:15:05Z")

</div>

Salut David,  
J'ai effacé l'index et recréé ainsi mais maintenant lorsque je veux "post" des valeurs j'ai en retour:

`{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Rejecting mapping update to [loadresult] as the final mapping would have more than 1 type: [result, doc]"}],"type":"illegal_argument_exception","reason":"Rejecting mapping update to [loadresult] as the final mapping would have more than 1 type: [result, doc]"},"status":400}`

Je poste simplement un document ainsi:

```
        ContentResponse contentResponse = httpClient.newRequest( host, port ).scheme( scheme ) //
            .path( "/loadresult/result/" + loadResult.getUuid() ) //
            .content( new StringContentProvider( stringWriter.toString() ) ) //
            .method( HttpMethod.PUT ) //
            .header( "Content-Type", "application/json" ) //
            .send();

```

Je continue de chercher 🙂

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 10, 2018, 12:50am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/6 "2018-04-10T00:50:06Z")

</div>

bon le secret est dans la typo 🙂  
`curl -s -XPUT 'localhost:9200/loadresult/_mapping/doc?pretty'`  
au lieu de  
`curl -s -XPUT 'localhost:9200/loadresult/_mapping/_doc?pretty'`

je ne suis pas sur de bien comprendre le \_doc vs le doc 🙂  
merci!

---

<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 10, 2018, 1:10am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/7 "2018-04-10T01:10:14Z")

</div>

En fait ce paramètre doc ou \_doc est le nom du type.

Dans les anciennes versions nous supportions plusieurs types par index.  
C'est en train d'être supprimé progressivement.

En v6, tu ne peux avoir qu'un seul type. Donc tu peux indexer en faisant:

```
PUT index/type/1
{ ... }

```

Mais si derrière tu fais:

```
PUT index/type_nouveau/1
{ ... }

```

Tu auras un rejet car cela reviendrait à avoir 2 types dans le même index.

Par convention, on utilise maintenant `_doc` comme nom de type. Mais j'ai la fâcheuse habitude d'utiliser `doc` (qui était la convention précédente).

le but est que dans le futur on migre vers:

```
PUT index/1
{ ... }
```

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 10, 2018, 10:11am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/8 "2018-04-10T10:11:56Z")

</div>

en tout cas tout fonctionne super!!  
Merci pour l'aide!

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 10, 2018, 11:31am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/9 "2018-04-10T11:31:48Z")

</div>

En fait un petit probleme 🙂  
Le champs dont je cherche les valeurs distinctes peut contenir:  
`"jettyVersion" : "9.4.8.v20171121",`

En faisant:

```
curl -s -XGET 'localhost:9200/loadresult/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "version": {
      "terms": {
        "field": "serverInfo.jettyVersion"
      }
    }
  }
}

```

Pour 9.4.8.v20171121 J'obtiens un entree 9.4.8 et une v20171121

"buckets" : [  
{  
"key" : "9.4.8",  
"doc\_count" : 3  
},  
{  
"key" : "v20171121",  
"doc\_count" : 3  
},  
{  
"key" : "9.1",  
"doc\_count" : 1  
},  
{  
"key" : "9.2",  
"doc\_count" : 1  
}  
]

---

<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 10, 2018, 11:45am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/10 "2018-04-10T11:45:39Z")

</div>

Quel est le mapping ?

```
GET yourindexname/_mapping
```

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 10, 2018, 11:54am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/11 "2018-04-10T11:54:32Z")

</div>

Pour ce champs

```
      "jettyVersion" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        },
        "fielddata" : true
      }
```

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 10, 2018, 11:58am UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/12 "2018-04-10T11:58:55Z")

</div>

elasticsearch version 6.2.3

---

<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 10, 2018, 12:47pm UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/13 "2018-04-10T12:47:06Z")

</div>

Run the agg on `jettyVersion.keyword`

---

<div class="post-metadata">

### Author: ![olamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olamy/32/29756_2.png) [@olamy](https://discuss.elastic.co/u/olamy)
#### Post date: [April 10, 2018, 9:16pm UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/14 "2018-04-10T21:16:22Z")

</div>

Super it works!!  
Merci!!

---

<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 8, 2018, 9:16pm UTC](https://discuss.elastic.co/t/recherche-de-valeur-distincte-dun-champs/127308/15 "2018-05-08T21:16:29Z")

</div>

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