6

With Spring-Data-Elasticsearch, I am trying to use analyzers and mappings defined in elasticsearch_config.json.
This JSON file is in /src/main/resources folder.

My JAVA model looks like:

@Document(indexName = "test", type="Tweet")
@Setting(settingPath = "/elasticsearch_config.json")
public class Tweet {   

    @Id
    private String idStr;

    /** other fields, getters and setters are omitted **/

}

elasticsearch_config.json contains both settings and mappings:

{
    "settings": { /* some filters */},
    "mappings": { /* some types' mappings*/ }
}

I tried with curl, I get no problem indexing/searching.

My problem:

I want to use @Setting to configure my mappings (not curl), but @Setting annotation doesn't seem to work. With @Setting, when I use curl to check my mapping, I don't get all the mappings I defined in elasticsearch_config.json :

curl -X GET "localhost:9200/test/_mapping?pretty=true"

My guess is that Spring can't find the elasticsearch_config.json properly. However I have already checked that myproject/src/main/resources is in my project's build path...

I appreciate any help, thank you!

Notes: I have found this solution that may work, but:
1. I don't understand where nodeBuilder comes from
2. I may be wrong but, isn't there a solution only using @Setting?

UPDATES: My mappings I separeted analyzers from mappings. mappings.json looks like this:

{
  "mappings": {
    "Tweet": {
      "_id": {
        "path":"idStr",
        "properties": {
          "idStr": {
            "type":"string",
            "index":"not_analyzed",
            "store":true
          }
        }
      },
      "numeric_detection" : true,
      "dynamic_templates": [
       {
          "id_fields": {
          "match":"userId*",
            "match_mapping_type": "string",
            "mapping": {
              "type":"string",
              "index":"no",
              "store":true
            }
          }
        },
        {
          "name_fields": {
            "match":"*Name",
            "match_mapping_type": "string",
            "mapping": {
              "type":"string",
              "index":"no",
              "store":true
            }
          }
        }
      ],
      "properties": {
        "text": {
          "type": "string",
          "index":"analyzed",
          "analyzer":"custom_analyzer",
          "store": true
        },
        "createdAt": {
          "type": "date",
          "format": "yyyy-MM-dd",
          "index":"analyzed",
          "store": true
        },
        "testVersion": {
          "type": "integer",
          "index":"not_analyzed",
          "store":false
        }
      }

    }

  }
}

2 Answers 2

17

Finally found out why it was not working!!

Like Val said, I decomposed my elasticsearch_config.json file into settings.json and mappings.json.

My project/src/main/ressources architecture:

- mappings
    + mappings.json
- settings
    + settings.json

And

@Document(indexName = "test", type="SentimentTweet")
@Setting(settingPath = "/settings/settings.json")
@Mapping(mappingPath = "/mappings/mappings.json")

However, in mappings.json, I should omit the field mappings and DIRECTLY put the content of the mapping.

INSTEAD OF:

{
    "mappings": {
        "Tweet": {
             /* MAPPINGS CONFIGURATION ARE OMITTED */
         }
    }
}

Only writes in mappings.json:

{
    "Tweet": {
         /* MAPPINGS CONFIGURATION ARE OMITTED */
     }
}

The same should be done for settings.json

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

1 Comment

Glad you figured it out. Do you confirm that in your mapping you have Tweet as the type and in the @Document annotation you use SentimentTweet or you have the same type in both places?
8

The @Setting annotation should point to a file containing only the settings part. If you also want to specify your custom mapping, you need to use the @Mapping annotation and give it the path to your mapping file. It goes like this:

@Document(indexName = "test", type="Tweet")
@Setting(settingPath = "/settings/settings.json")
@Mapping(mappingPath = "/mappings/mappings.json")
public class Tweet {   

    @Id
    private String idStr;

    /** other fields, getters and setters are omitted **/

}

Then you need to store settings.json in myproject/src/main/resources/settings/ and mappings.json in myproject/src/main/resources/mappings/.

That should work.

4 Comments

Thank you, but that's still not working.. When I check my mappings with curl, I get mapping for only 3 fields, but my json contains mapping for around 10 fields.
Out of curiosity, can you show your mappings.json file?
Thanks, then in the @Document annotation you should use SentimentTweet instead of Tweet. Please try it out.
Sorry, that's still not working... Even the custom analyzer that I defined in settings.json defined with @Setting cannot be found (when verifying if @Setting worked, with curl I got: failed to find analyzer [custom_analyzer])

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.