Spring Data Elasticsearch:使用注释的设置和映射配置不起作用

9
我正在使用Spring Boot嵌入式Elasticsearch,并尝试使用注释配置设置和映射。我遵循了这个Tutorial,它解释了如何在多个字段上实现搜索。无论如何,我已经按照此处所述,在我的文档实体中定义了settings.json和mappings.json,但似乎没有读取文件,因为curling映射不会返回与文件中定义的相应配置。

索引由Spring批处理进程执行。它从数据库读取数据并将其写入Elasticsearch。这个工作完美。

当我向http://localhost:9200/profile/_search发出POST请求时,我没有得到结果(应该返回7个项目):

{
   "size": 10,
   "query": {
       "match": {
       "_all": {
           "query": "user male",
           "operator": "and"
       }
    }
  }
}

如果我将查询更改为“user5”,它会返回具有此昵称的用户。
如果有人能给我一个提示,我会很感激。配置有什么问题?
配置
@Configuration
@EnableElasticsearchRepositories(basePackages ="com.company.searchengine.repository")
public class ElasticSearchConfiguration {
    @Value("${elasticsearch.clustername}")
    private String esClusterName;

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws IOException {
        return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName
            (esClusterName).node()
            .client());
    }
}

该文档

@EqualsAndHashCode(of = "uuid")
@ToString(exclude = "uuid")
@NoArgsConstructor(onConstructor = @__({@JsonCreator}))
@Getter
@Setter
@Document(indexName = "profiles", type = "profile", createIndex = false)
@Setting(settingPath = "/settings/settings.json")
@Mapping(mappingPath = "/mappings/mappings.json")
public class IndexedProfile {
    @Id
    @NonNull
    private String uuid;
    @NonNull
    //@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
    private String nickname;
    @NonNull
    //@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
    private String gender;
    //@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
    private String about;
    private GeoPoint location;
    @Field(type = FieldType.Date, format = DateFormat.year_month_day)
    private Date birthdate;
}

批处理作业

@Override
public void write(List<? extends IndexedProfile> items) throws Exception {
    List<IndexQuery> indexQueries = items.stream()
            .map(item -> new IndexQueryBuilder().withObject(item).withId(String.valueOf(item.getUuid())))
            .map(builder -> builder.withType(DOCUMENT_TYPE))
            .map(builder -> builder.withIndexName(INDEX_NAME + runId))
            .map(IndexQueryBuilder::build)
            .collect(Collectors.toList());

    this.elasticsearchTemplate.bulkIndex(indexQueries);
}

设置

{
  "settings": {
       "analysis": {
           "filter": {
               "nGram_filter": {
                  "type": "nGram",
                   "min_gram": 2,
                   "max_gram": 20,
                   "token_chars": [
                        "letter",
                        "digit",
                        "punctuation",
                        "symbol"
                    ]
            }
        },
        "analyzer": {
             "nGram_analyzer": {
                 "type": "custom",
                 "tokenizer": "whitespace",
                 "filter": [
                      "lowercase",
                      "asciifolding",
                      "nGram_filter"
                 ]
        },
        "whitespace_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
                  "lowercase",
                  "asciifolding"
             ]
         }
      }
    }
  }
}

映射

{
  "mappings": {
      "profile": {
          "_all": {
               "index_analyzer": "nGram_analyzer",
               "search_analyzer": "whitespace_analyzer"
           },
           "nickname": {
                "type": "string",
                "index": "not_analyzed"
           },
           ....
       }
  }
3个回答

4
根据org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity,设置createIndex=false将导致框架不创建索引和映射!因此,您应该删除该设置或将其设置为true。


1
studentdoc_setting_index_mapping_type_overlayadjacency.json
{
        "index": {
            "mapping": {
                "total_fields": {
                    "limit": "100000"
                }
            }   
    }
}

@Setting(settingPath = "studentdoc_setting_index_mapping_type_overlayadjacency.json")
public class StudentDoc {
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接