5

Actually my question is very simple: I want my hashmap value not_analyzed!

Now i have one object contains a hashmap[string, string], looks like:

class SomeObject{
    String id;
    @Field(type=FieldType.Object, index=FieldIndex.not_analyzed)
    Map<String, String> parameters;
}

then Spring data elasticsearch generate the mapping like this at the beggining:

{
    "id": {
      "type": "string"
    },
    "parameters": {
      "type": "object"
    }
}

then after i add some objects to the es, it add more attributes like this:

{
    "id": {
      "type": "string"
    },
    "parameters": {
        "properties": {
             "shiduan": {
                "type": "string"
             },
             "季节": {
                "type": "string"
             }
        }
    }
}

now, because of the parameters's value is analyzed, so cannot be search by es, i mean cannot search chinese value, i have tried i can search english at this time.

THEN, AFTER READ THIS POST https://stackoverflow.com/a/32044370/4148034, I UPDATE THE MAPPING MANUALLY TO THIS:

{
    "id": {
      "type": "string"
    },
    "parameters": {
        "properties": {
             "shiduan": {
                "type": "string",
                "index": "not_analyzed"
             },
             "季节": {
                "type": "string",
                "index": "not_analyzed"
             }
        }
    }
}

I CAN SEARCH CHINESE NOW, SO I KNOW THE PROBLEM IS "not_analyzed", LIKE THE POST SAID.

Finally, anyone can tell me how to make the map value "not_analyzed", i had google and stackoverflow many many times still cannot find the answer, let me know if someone can help, thanks very much.

1 Answer 1

9
+50

One way to achieve this is to create a mappings.json file on your build path (e.g. yourproject/src/main/resources/mappings) and then reference that mapping using the @Mapping annotation in your class.

@Document(indexName = "your_index", type = "your_type")
@Mapping(mappingPath = "/mappings/mappings.json")
public class SomeObject{
    String id;
    @Field(type=FieldType.Object, index=FieldIndex.not_analyzed)
    Map<String, String> parameters;
}

In that mapping file, we're going to add a dynamic template which will target the subfields of your parameters hashmap and declare them to be not_analyzed string.

{
  "mappings": {
    "your_type": {
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "string",
            "path_match": "parameters.*",
            "mapping": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      ]
    }
  }
}

You need to make sure to delete your_index first and then restart your application so it can be recreated with the proper mapping.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for not accept your answer on time, it works. Thanks again for your help 😁
@val Is there a newer approach to this or do I still have to do it this way? Thanks for your prev. answer ;)
@FabianTe not that I know of even though there was this PR but it never got merged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.