代码之家  ›  专栏  ›  技术社区  ›  steven

如何利用spring数据弹性搜索映射连接类型

  •  0
  • steven  · 技术社区  · 6 年前

    我将数据从es 2.4重新索引到5.6。
    ES 2.4中的数据有2种类型,2种类型为父子关系。
    当将其重新索引到es 5.6时,该索引只包含单一类型,即通过使用join type来解析的父子关系。
    上面的数据工作正常。 映射示例如下所示,它包含一个联接类型:

    "mappings": {
        "doc": {
            "properties": {
                "my_join_field": {
                    "eager_global_ordinals": true,
                    "type": "join",
                    "relations": {
                        "question": "answer"
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
    

    如何使用Spring数据弹性搜索映射联接类型: 在旧版本代码es 2.4中,我可以如下映射:

    @Document(indexName = ParentEntity.INDEX, type = ParentEntity.PARENT_TYPE, shards = 1, replicas = 0, refreshInterval = "-1")
    public class ParentEntity {
    
        public static final String INDEX = "parent-child";
        public static final String PARENT_TYPE = "parent-entity";
        public static final String CHILD_TYPE = "child-entity";
    
        @Id
        private String id;
        @Field(type = FieldType.Text, store = true)
        private String name;
    
        public ParentEntity() {
        }
    
        public ParentEntity(String id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public String getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public String toString() {
            return new ToStringCreator(this).append("id", id).append("name", name).toString();
        }
    
        @Document(indexName = INDEX, type = CHILD_TYPE, shards = 1, replicas = 0, refreshInterval = "-1")
        public static class ChildEntity {
    
            @Id
            private String id;
            @Field(type = FieldType.Text, store = true)
            @Parent(type = PARENT_TYPE)
            private String parentId;
            @Field(type = FieldType.Text, store = true)
            private String name;
    
            public ChildEntity() {
            }
    
            public ChildEntity(String id, String parentId, String name) {
                this.id = id;
                this.parentId = parentId;
                this.name = name;
            }
    
            public String getId() {
                return id;
            }
    
            public String getParentId() {
                return parentId;
            }
    
            public String getName() {
                return name;
            }
    
            @Override
            public String toString() {
                return new ToStringCreator(this).append("id", id).append("parentId", parentId).append("name", name).toString();
            }
        }
    }
    

    如何使用Spring Data ElasticSearch v3.0.10映射联接类型?

    今天,我在Spring Data ElasticSearch 3.0.10中尝试了以下实体:

    @Document(indexName = "join_index", type = "join_mapping")
    @Data
    public class JoinEntity {
        @Id
        private String id;
        @Mapping(mappingPath = "/mappings/join_type.json")
        private Map<String,String> relationType;
        @Field(type = FieldType.Keyword)
        private String name;
        //@Parent(type = "question")
        @Field(type = FieldType.Keyword)
        private String parentId;
    }
    

    下面是join_type.json:

    {
      "type": "join",
      "relations": {
        "question": "answer"
      }
    }
    

    它创建索引并使映射工作正常:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:elasticsearch-template-test.xml")
    public class ElasticsearchTemplateJoinTests {
    
        @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;
    
        @Before
        public void before() {
            clean();
            elasticsearchTemplate.deleteIndex(JoinEntity.class);
            elasticsearchTemplate.createIndex(JoinEntity.class);
            elasticsearchTemplate.putMapping(JoinEntity.class);
            elasticsearchTemplate.refresh(JoinEntity.class);
        }
    
        @Test
        public void shouldCreateIndexAndMappingSuccess(){
            Map mapping = elasticsearchTemplate.getMapping(JoinEntity.class);
            assertThat(mapping, is(notNullValue()));
    
            Map properties = (Map) mapping.get("properties");
            assertThat(properties, is(notNullValue()));
    
            assertThat(properties.containsKey("name"), is(true));
            Map file = (Map) properties.get("relationType");
            assertThat(file, is(notNullValue()));
            assertThat(((String) file.get("type")), is("join"));
        }
    }
    

    当索引父项也可以工作,但索引子项会引发异常:

    @Test 
    public void shouldIndexParentAndChildSuccess(){
        JoinEntity parenEntity = new JoinEntity();
        parenEntity.setName("parent_name");
        parenEntity.setRelationType(Collections.singletonMap("name","question"));
        IndexQuery parentQuery = new IndexQueryBuilder().withId("11").withObject(parenEntity).build();
        final String id = elasticsearchTemplate.index(parentQuery);
        assertThat("11",is(id));
        JoinEntity childEntity = new JoinEntity();
        childEntity.setName("child_name");
        Map<String,String> joinRelation = new HashMap<>(2);
        joinRelation.put("name","answer");
        joinRelation.put("parent", "11");
        childEntity.setRelationType(joinRelation);
        childEntity.setParentId("11");
        IndexQuery childQuery = new IndexQueryBuilder().withId("22").withObject(childEntity).build();
        elasticsearchTemplate.index(childQuery);
    }
    

    例外情况:

    MapperParsingException[failed to parse
    ]; nested: IllegalArgumentException[[routing] is missing for join field [relationType]];
        at org.elasticsearch.index.mapper.DocumentParser.wrapInMapperParsingException(DocumentParser.java:171)
    

    如何解决此问题或正确映射新版本的父子关系?厚!啊!

    0 回复  |  直到 6 年前