问题
Running a java elastic search query through Eclipse. Getting this error when I am testing it, and I can't find anywhere in the API that tells me how I can set this field data to true
IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [created] 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.]
Does anyone know how I can fix this?
回答1:
You must modify index mapping properties:
try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
final XContentBuilder builder = jsonBuilder
.startObject()
.startObject("your_type")
.startObject("properties")
.startObject("your_field")
.field("type", "text")
.field("fielddata", true)/*setting fielddata*/
.endObject()
.endObject()
.endObject()
.endObject();
client.admin().indices().preparePutMapping("your_index")
.setType("your_type")
.setSource(builder)/*also there are overloads for setSource()*/
.get();
}
OR
String source = "{\"your_type\":{\"properties\":{\"your_field\":{\"type\":\"text\",\"fielddata\":true}}}}";
client.admin().indices().preparePutMapping("your_index)
.setType("your_type")
.setSource(source, XContentType.JSON)
.get();
Result:
{
"your_index": {
"aliases": {},
"mappings": {
"your_type": {
"properties": {
...
...
"your_field": {
"type": "text",
"fielddata": true,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
...
...
}
}
}
}
}
回答2:
In Elasticsearch 5.0, String fields were broken into two - Text and Keyword. If the property is supposed to be Analyzed, you'd want to migrate your String fields to Text, if not then migrate them to Keyword.
Look into your type mapping - If you had a property like this -
{
"your_field": {
"type" "string",
"index": "not_analyzed"
}
}
You'd want to convert it to this -
{
"your_field": {
"type" "keyword",
"index": true
}
}
Similarly, if your property was supposed to be analyzed and you had it like this-
{
"your_field": {
"type" "string",
"index": "analyzed"
}
}
then you'd want to convert it to this-
{
"your_field": {
"type" "text",
"index": true
}
}
Details on Elasticsearch official page
来源:https://stackoverflow.com/questions/44447205/set-field-data-true-on-java-elasticsearch