Elasticsearch搜索单字母Java Api - 1.7

时间:2017-02-23 17:04:03

标签: java search elasticsearch indexing

Hello Elasticsearch Friends。

我在Elasticsearch Java API中遇到了设置和映射问题。我配置了索引并设置了映射和设置。我的索引名称是“orange11”,键入“profile”。我希望当我在2或3个字母后输入我的搜索inputField时,Elasticsearch会给我一些结果。我已经阅读了有关分析器,映射和所有这些内容的内容,因此我尝试了。

这是我的IndexService代码:

@Service
public class IndexService {
private Node node;
private Client client;

@Autowired
public IndexService(Node node) throws Exception {
    this.node = node;

    client = this.node.client();

    ImmutableSettings.Builder indexSettings = ImmutableSettings.settingsBuilder();

    indexSettings.put("orange11.analysis.filter.autocomplete_filter.type", "edge_ngram");
    indexSettings.put("orange11.analysis.filter.autocomplete_filter.min.gram", 1);
    indexSettings.put("orange11.analysis.filter.autocomplete_filter.max_gram", 20);
    indexSettings.put("orange11.analysis.analyzer.autocomplete.type", "custom");
    indexSettings.put("orange11.analysis.analyzer.tokenizer", "standard");
    indexSettings.put("orange11.analysis.analyzer.filter", new String[]{"lowercase", "autocomplete_filter"});


    IndicesExistsResponse res = client.admin().indices().prepareExists("orange11").execute().actionGet();
    if (res.isExists()) {
        DeleteIndexRequestBuilder delIdx = client.admin().indices().prepareDelete("orange11");
        delIdx.execute().actionGet();
    }
    CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("orange11").setSettings(indexSettings);


    // MAPPING GOES HERE
    XContentBuilder mappingBuilder = jsonBuilder().startObject().startObject("profile").startObject("properties")
            .startObject("name").field("type", "string").field("analyzer", "autocomplete").endObject()
            .endObject()
            .endObject();
    System.out.println(mappingBuilder.string());
    createIndexRequestBuilder.addMapping("profile ", mappingBuilder);

    createIndexRequestBuilder.execute().actionGet();

    List<Accounts> accountsList = transformJsonFileToJavaObject();

    //Get Data from jsonMap() function into a ListMap.
    //List<Map<String, Object>> dataFromJson = jsonToMap();

    createIndex(accountsList);

}
public List<Accounts> transformJsonFileToJavaObject() throws IOException {
    ObjectMapper mapper = new ObjectMapper();

    List<Accounts> list = mapper.readValue(new File("/Users/lucaarchidiacono/IdeaProjects/moap2/MP3_MoapSampleBuild/data/index/testAccount.json"), TypeFactory.defaultInstance().constructCollectionType(List.class, Accounts.class));

    return list;
}


public void createIndex(List<Accounts> accountsList) {
    for (int i = 0; i < accountsList.size(); ++i) {

        Map<String, Object> accountMap = new HashMap<String, Object>();

        accountMap.put("id", accountsList.get(i).getId());
        accountMap.put("isActive", accountsList.get(i).isActive());
        accountMap.put("balance", accountsList.get(i).getBalance());
        accountMap.put("age", accountsList.get(i).getAge());
        accountMap.put("eyeColor", accountsList.get(i).getEyeColor());
        accountMap.put("name", accountsList.get(i).getName());
        accountMap.put("gender", accountsList.get(i).getGender());
        accountMap.put("company", accountsList.get(i).getCompany());
        accountMap.put("email", accountsList.get(i).getEmail());
        accountMap.put("phone", accountsList.get(i).getPhone());
        accountMap.put("address", accountsList.get(i).getAddress());
        accountMap.put("about", accountsList.get(i).getAbout());
        accountMap.put("greeting", accountsList.get(i).getGreeting());
        accountMap.put("favoriteFruit", accountsList.get(i).getFavoriteFruit());
        accountMap.put("url", accountsList.get(i).getUrl());


        //Request an Index for indexObject. Set the index specification such as indexName, indexType and ID.
        IndexRequestBuilder indexRequest = client.prepareIndex("orange11", "profile", Integer.toString(i)).setSource(accountMap);

        //Execute the indexRequest and get the result in indexResponse.
        IndexResponse indexResponse = indexRequest.execute().actionGet();

        if (indexResponse != null && indexResponse.isCreated()) {
            //Print out result of indexResponse
            System.out.println("Index has been created !");
            System.out.println("------------------------------");
            System.out.println("Index name: " + indexResponse.getIndex());
            System.out.println("Type name: " + indexResponse.getType());
            System.out.println("ID: " + indexResponse.getId());
            System.out.println("Version: " + indexResponse.getVersion());
            System.out.println("------------------------------");
        } else {
            System.err.println("Index creation failed.");
        }
    }
}


}

每次我想运行此代码时,我都会成为例外:

Caused by: org.elasticsearch.index.mapper.MapperParsingException: Root type mapping not empty after parsing! Remaining fields:   [profile : {properties={name={analyzer=autocomplete, type=string}}}]
at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:278)
at org.elasticsearch.index.mapper.DocumentMapperParser.parseCompressed(DocumentMapperParser.java:192)
at org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:449)
at org.elasticsearch.index.mapper.MapperService.merge(MapperService.java:307)
at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$2.execute(MetaDataCreateIndexService.java:391)

我不知道如何继续,因为我没有看到任何'。'我的indexSettings中缺少。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

min.gram更改为min_gram (见indexSettings.put("orange11.analysis.filter.autocomplete_filter.min.gram", 1);