spring data elasticsearch:注释无效的设置和映射配置

时间:2017-05-28 17:42:42

标签: elasticsearch spring-data-elasticsearch

我使用嵌入式弹性搜索和弹簧启动,我试图使用注释来配置设置和映射。我按照这个Tutorial解释了如何在多个字段上实现搜索。无论如何,我已经在我的文档实体中定义了settings.json和mappings.json,如here所述,但似乎它没有读取文件,因为curl ing 映射不会返回文件中定义的相应配置。

索引已由弹簧批处理执行。它从数据库中读取数据并将其写入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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

答案 2 :(得分:0)

z=x+y