0

I'm "almost" new on Elastic Search. I've been using it for a while but never used Analyzers before.

I can make a full text search on my project but the problem is, when I try to find a name like "Alex", I should completely type down the name correcly. It doesn't work with "Al" or "Ale". It says something like "no match found".

I found some source codes from different sites, but it makes me confused.

What should I do is:

1) Creating a nGram tokenizer

2) Then mapping it with my all indexes?

I have lots of indexes already created and I got errors while creating a mapping on them.

Should I create my analyzer settings and mapping very in the beggining just before indexing my records ?

I'm working on a Java project, so answers on JAVA API will be very appreciated.

Thanks a lot!

BurakT
  • 1
  • 1
  • 1

1 Answers1

0

mappings should always be created first and then the data should be indexed. if possible, delete your old indices and recreate with new mapping. if you are concerned about loosing your data, then just create a new type for an existing index. the new type can use the new mapping.

for example, here is a piece that uses the Java API to create a custom mapping

public class MappingCreator {

    static Logger log = Logger.getLogger(MappingCreator.class.getName());

    final static String indexName =  "indexName";

    final static String typeName = "typeName";

    final static String mappingFileName = "pathToMapping.jsonFile";

    final static String clusterName = "elasticsearch"; // or name of your cluster

    final static String hostName = "localhost";

    public static void main(String args[]) throws IOException
    {

        MappingCreator mapCreator = new MappingCreator();

        Client myESclient = getClient();

        IndicesExistsResponse res = myESclient.admin().indices().prepareExists(indexName).execute().actionGet();

        if (res.isExists()) {

            log.warn("Index "+indexName +" already exists. Will be deleted");

            final DeleteIndexRequestBuilder deleteIndexBuilder = myESclient.admin().indices().prepareDelete(indexName);

            deleteIndexBuilder.execute().actionGet();
        }

        final CreateIndexRequestBuilder createIndexBuilder = myESclient.admin().indices().prepareCreate(indexName)
                .addMapping(typeName, mapCreator.getIndexFieldMapping());

        CreateIndexResponse createIndexResponse = createIndexBuilder.execute().actionGet();

        log.debug("Created mapping "+createIndexResponse.toString());

        myESclient.close();

    }

    private String getIndexFieldMapping() throws IOException {

        return IOUtils.toString(getClass().getClassLoader().getResourceAsStream(mappingFileName));
    }

    private static Client getClient() {

        TransportClient transportClient = null;

        try
        {
            Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build();

            transportClient = new TransportClient(settings);

            transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress(hostName, 9300)); 

/* be very careful about the port number here. by default its 9300. note that this is the TCP port which the java api will use. unlike the http port which is 9200 */

        }
        catch (Exception e)
        {
            log.error("Error in MappingCreator creating Elastic Search Client\n"
                    + "Message "+e.getMessage()+"\n"
                            + "StackTrace "+e.getStackTrace()
                    );
        }

        return (Client) transportClient;

    }

}

i hope this helps. by the way its really cool that you are making your own nGram tokenizer. i would love to see the code for that and how it is done :)

AbtPst
  • 7,778
  • 17
  • 91
  • 172