How to setup Mapping and IndexSetting for searching just single letter in Java

Hello Elasticsearch friends.
I'm new in the elastcsearch world and can't continue my work.
At the moment I'm working with Elasticsearch 1.7, because in our company we can't use the other versions.
I implemented Elasticsearch into a Java application. I don't have the standalone server. I created a Node FactoryBean so that I don't have to run the Elasticsearch server. Anyway...
I started to create a Java Project and everything was fine till i wanted to search and match words with single letters which I type in a search input field. But I don't get any result from the compiler and from the html front site. Hits are every time []. When I type-in "joy" I don't get the result "Joyce". But when I type-in the full word "joyce" the compiler gives me the response hits[1] and returns the hit "Joyce". I think that something is bad with my mapping and settings in Index but I don't know what is wrong.

For the indexing I created a class and wrote this code:

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

    client = this.node.client();

    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(ImmutableSettings.settingsBuilder().loadFromSource(jsonBuilder()
                    .startObject()
                    .startObject("analysis")
                    .startObject("filter")
                    .startObject("autocomplete_filter").field("type", "edge_ngram").field("min_gram", 1).field("max_gram", 50).endObject()
                    .endObject()
                    .startObject("analyzer")
                    .startObject("autocomplete").field("type", "custom").field("tokenizer", "standard").field("filter", new String[]{"lowercase", "autocomplete_filter"}).endObject()
                    .endObject()
                    .endObject()
                    .endObject().string()));

    XContentBuilder mappingBuilder = jsonBuilder().startObject().startObject("properties")
            .startObject("name").field("type", "string").field("index_analyzer", "autocomplete").field("search_analyzer", "standard").endObject()
            .endObject()
            .endObject();

    createIndexRequestBuilder.addMapping("profile", mappingBuilder);

    createIndexRequestBuilder.execute().actionGet();

    List<Accounts> accountsList = transformJsonFileToJavaObject();

    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());

        IndexRequestBuilder indexRequest = client.prepareIndex("orange11", "profile", Integer.toString(i)).setSource(accountMap);

        IndexResponse indexResponse = indexRequest.execute().actionGet();

        if (indexResponse != null && indexResponse.isCreated()) {
            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.");
        }
    }
}
}

This is the query which I send to my SearchController:

 '{"size" : 10, "from" : 0, "query" : { "match" : { "_all" : {"query" : "' + searchWordInInputField + '"}}}, "highlight" : {"fields" : { "*" : {}}, "require_field_match" : false}}'

This is my SearchController:

Client client = node.client();
    List<Hit> esSearchResult = new ArrayList<Hit>();

    SearchRequestBuilder searchRequest = client.prepareSearch("orange11").setTypes("profile").setSource(query);

    SearchResponse searchResponse = searchRequest.execute().actionGet();

    System.out.println("Hits: " + searchResponse.getHits());
    System.out.println("Response1: wichtige Info: " + searchResponse.toString());

    for (SearchHit hit : searchResponse.getHits()) {

        Hit hitDto = new Hit(hit.getSource());

        Map<String, HighlightField> highlightFields = hit.getHighlightFields();

        for (String keyValue : highlightFields.keySet()) {
            List<String> fragmentsDto = new ArrayList<>();

            Text[] textFragments = highlightFields.get(keyValue).fragments();

            for (Text fragment : textFragments) {
                fragmentsDto.add(fragment.toString());
            }
            hitDto.addField("_all", fragmentsDto);
        }
        esSearchResult.add(hitDto);
    }
    return esSearchResult;
}
}

I hope I get some help from you pro's.
And sorry for my english.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.