4
votes

I think this is a very basic question but I keep going around the houses so any help pointing me in the right direction would be appreciated.

I have inherited a java application which builds and elastic search index using spring-data-elasticsearch (1.2.1.RELEASE at the moment). I have modified this quite successfully in various trivial ways but now I want to add a custom analyzer to use on one field (char mapping to remove /'s).

The index being built is essentially 1 index with various document types. It seems to be built pretty much out of the box. I'm fairly new to java and spring but and tracking down all the config and auto-wiring can still outfox me sometimes but as far as I can see the client config in the context XML file points directly to the spring code and doesn't add much except the custom index name and a location for the repository interfaces and code

<elasticsearch:node-client id="esClient" local="true" cluster-name="products"/>
<elasticsearch:repositories base-package="com.warehouse.es.repos"/>

<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
    <constructor-arg name="client" ref="esClient"/>
</bean>

The code then seems to use an out of the box client object

@Autowired
public void setClient(Client client) throws IOException {
    this.client = client;

and then goes on to set various typemappings using mapping files along these lines

createTypeMapping(client, Constants.INDEX_NAME, INDEX_TYPE, "Products.mapping");

Apologies if some of this is either too brief (or too much waffle for this basic question) but I'm trying to work out / find an example of how and where to add my custom analyzer. I have documentation and examples to show me how to create some json to create the custom analyzer (e.g. https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-mapping-charfilter.html#analysis-mapping-charfilter and some previous stackoverflow q&s's)

but I'm struggling to understand where I add this in my java code creating the index.

Obviously the more help the better (!) but really at this stage I'm trying to get a grip of whether I could just add the analyzer to the yml file or whether I need to add some code to modify the client in some way ? or possibly even just add it to the individual type mappings.

Thanks.

1
Do you have access to the ES cluster itself? In your java code do you have anywhere any *.json files that might define a mapping?Andrei Stefan
I have access to the cluster. In this case it is written to a stand alone cluster in the target folder and I then copy it (in dev environment) into my local es data folder. There are a number of .mapping files which define types but nothing which defines the index at a higher level. Maybe I can just add a custome analyzer at this type level ? e.g Products.mapping { "properties": { "created": { "type": "long" }, "Summary": { "type": "string", "analyzer": "standard" }, "relatedProducts" : { ...gringogordo
What I'm trying to decide is if the index/type has been created directly on the cluster (like running a curl command) or if the index/types creation is handled by your Spring application. In the latter case, I think you can follow some code samples from this link on github.Andrei Stefan
Yes, if you have Products.mapping with that content that looks like a type mapping indeed. Any data that you have in your cluster, though, needs to be re-indexed if you change the type mapping by adding a new analyzer.Andrei Stefan
Okay great, so I can add it to the type mapping file and Spring will do the rest ?! Many thanks.gringogordo

1 Answers

1
votes

If the index/type has been created directly on the cluster (like running a curl command) or if the index/types creation is handled by your Spring application. In the latter case, I think you can follow some code samples from this link on github.

If you have Products.mapping file with that content that looks like a type mapping indeed. Any data that you have in your cluster, though, needs to be re-indexed if you change the type mapping by adding a new analyzer.

EDIT with poster findings: it is not possible to put the settings in project's individual mapping files, but as a separate file with the @settings annotation.