如何在Elasticsearch中为Java ArrayList创建索引映射

时间:2018-07-15 11:53:08

标签: elasticsearch elasticsearch-6

具有如下所示的模型类,

class Search{

int id,
List<User> users;
}

class User{
int id,
String name;
}

对于上面提到的类,我想在elasticSearch中创建映射,并想知道如何实现该映射。Googled遍地都是,但是我什么也没得到,因为我对Elasticsearch非常陌生。

我在下面粘贴了一个不完整的索引映射,并请求帮助以在Elasticsearch中实现完整的模型映射。

PUT user_index
{
    "mappings": {
        "user_map": {
            "properties": {
                "id": {
                    "type": "integer"
                }
            }
        }
    }   
}

1 个答案:

答案 0 :(得分:0)

使用户类型为$<$<OR:0,0,1>:YAY!>,然后在其中添加其余属性。即使不添加用户属性,它也会自动映射它们。重要的是使用户嵌套。

public static void main(String[] args) {
    String s = startChar("Apple", 'p');
    System.out.println("");
}

public static String startChar(String str, char ch) {
    return startChar(str,ch,"","");
}

private static String startChar(String str, char ch, String acc, String chs) {
    //This if statement details the end condition
    if (str.length() < 1) {
        return chs + acc;
    }


    String newString = str.substring(1); //Create new string without first character

    if(str.charAt(0) == ch) { //This happens when your character is found
        return startChar(newString, ch,acc, chs + ch);
    } else { //This happens with all other characters
        return startChar(newString, ch,acc+str.charAt(0), chs);
    }
}

引用:Using nested fields for arrays of objects