Basics of adding a custom analyzer to an index built using spring - elasticsearch

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.

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.

Related

Quarkus - #ConfigMapping: built-in way to show all properties like "toString()", instead of manual building

As #ConfigMapping uses interfaces, there are no ways to implement toString(); I cannot view all values and nested values without a lot of manual work(reflection and switch case to deal with each type).
Any plan to support easy view of all levels of properties? Like a super class to inherit which handles this manual toString() like building?
In SmallRye config doc page I read this:
ToString#
If the config mapping contains a toString method declaration, the config mapping instance will include a proper implementation of the toString method.
But I added #Override String toString(); method everywhere, Quarkus just complains about cannot find property "to_string".
OK I found this issue which is implemented in this commit, which exactly adds the sentence I read into the doc; but still not very clear to me.
Adding a String toString() method in your #ConfigMapping will generate the expected toString() implementation.
This is only available starting from SmallRye Config 2.11.0 and Quarkus 2.12.0.Final, which came out just a few weeks ago. Previous versions will just try to resolve the method as a configuration property. From your description, it seems that is the case, so you may be using an older Quarkus version that does not support this feature yet.

Programmatic way to build a hal+json from a list with certain object in spring-rest / Jackson

For testing purposes im searching for a elegant and less error prune way to build a hal+json data structure based on a Java list of certain objects.
Currently im using a quite huge ugly String for mapping/defining a expected hal+json data structure. I could place this of course also into a file but still imho its a bit error prune. As soon as a object/property would change i also would need to change my hard coded hal+json string/file...
Does anybody knows a helper class or something what could help to build the hal+json based on Java objects?
Spring HATEOAS helps you with generation of hal+json response .
you have to take care of following configuration
on any spring configuration class add #EnableHypermediaSupport(type = { HypermediaType.HAL })
2.make sure that you have Jackson library on classpath
3.Java object extends ResourceSupport or you wrap java object around Resource.
more details on Resource
4.This should generate hal+json response.
5.Add specific links to resource like self , to other resources .
Please click here for more details on Links

#Managedoperation alternative in XML configuration way in spring jmx

I am using JMX of spring Version 2.5 in which I am using JMX
as shown below..
#ManagedOperation(description = "Mark the Entry corresponding ABC flow")
#ManagedOperationParameters(value = {
#ManagedOperationParameter(name = "def", description = "Ids of the entries that needs to be STOP"),
#ManagedOperationParameter(name = "Comments", description = "Note on why these entries are being marked as stop") })
public void abcstop(String def, String gtr){
StringBuffer gfhtrPresent= jmxService.abcd(Ids, comments);
if(idsNotPresent.length()>0)
throw new IOARuntimeException("<font color=red><b>No data found for the following id/id's </b></font>"+idsNotPresent);
}
Now I want to remove the #Managedoperation annaotation and want to configure it with in XML , please advsie how can I configure the #Managedoperation , as i wan the same functionality to be run from xml itself, Please advise.
one way to achieve this is implement your own MBeanInfoAssembler (or subclass one of the standard ones). please advise is there any other way to achieve this, Any early help would be appreciated.
The simplest way might be to use a InterfaceBasedMBeanInfoAssembler.
First, expose the JMX interface as an explicitly-defined interface in your code. (Having such an interface is probably a good idea anyway.) Then you just tell the InterfaceBasedMBeanInfoAssembler to expose a particular interface (or interfaces) via its managedInterfaces property. Apart from the defining the interface in the first place (which you might or might not have already done) the rest is entirely possible from XML configuration. But you won't be able to supply very detailed metadata this way; it's a trade-off.
If you're going to stick with a MetadataMBeanInfoAssembler, you could instead try a custom JmxAttributeSource so that you're only reinventing half the wheel and not the whole lot…

How to add a custom ContentHander for JAXB2 support in Spring 3 (MVC)?

Scenario: I have a web application that uses Spring 3 MVC. Using the powerful new annotations in Spring 3 (#Controller, #ResponseBody etc), I have written some domain objects with #XML annotations for marhalling ajax calls to web clients. Everything works great. I declared my Controller class to have a return type #ResponseBody with root XML object - the payload gets marshalled correctly and sent to Client.
The problem is that some data in the content is breaking the XML compliance. I need to wrap this with CDATA when necessary. I saw a POST here How to generate CDATA block using JAXB? that recommends using a custom Content Handler. Ok, fantastic!
public class CDataContentHandler extends (SAXHandler|XMLSerializer|Other...) {
// see http://www.w3.org/TR/xml/#syntax
private static final Pattern XML_CHARS = Pattern.compile("[<>&]");
public void characters(char[] ch, int start, int length) throws SAXException {
boolean useCData = XML_CHARS.matcher(new String(c,start,length)).find();
if (useCData) super.startCDATA();
super.characters(ch, start, length);
if (useCData) super.endCDATA();
}
}
Using Spring MVC 3, how do I achieve this? Everything was "auto-magically" done for me with regards to the JAXB aspects of setup, Spring read the return type of the method, saw the annotations of the return type and picked up JAXB2 off the classpath to do the marshalling (Object to XML conversion). So where on earth is the "hook" that permits a user to register a custom Content Handler to the config?
Using EclipseLink JAXB implementation it is as easy as adding #XmlCDATA to the Object attribute concerned. Is there some smart way Spring can help out here / abstract this problem away into a minor configuration detail?
I know Spring isn't tied to any particular implementation but for the sake of this question, please can we assume I am using whatever the default implementation is. I tried the Docs here http://static.springsource.org/spring-ws/site/reference/html/oxm.html but it barely helped at all with this question from what I could understand.
Thanks all for any replies, be really appreciated.
Update:
Thanks for the suggested answer below Akshay. It was sufficient to put me on right tracks. Investigating further, I see there is a bit of history with this one between Spring version 3.05 and 3.2. In Spring 3.05 it used to be quite difficult to register a custom MessageConverter (this is really the goal here).
This conversation pretty much explains the thinking behind the development changes requested:
https://jira.springsource.org/browse/SPR-7504
Here is a link to the typically required class override to build a cusom solution:
http://static.springsource.org/spring/docs/3.1.0.M1/javadoc-api/org/springframework/http/converter/AbstractHttpMessageConverter.html
And the following Question on stack overflow is very similar to what I was asking for (except the #ResponseBody discussion relates to JSON and jackson) - the goal is basically the same.
Spring 3.2 and Jackson 2: add custom object mapper
So it looks like usage of , and overriding MarshallingHttpMessageConverter is needed, registering to AnnotationMethodHandlerAdapter. There is a recommended solution in link above to also get clever with this stuff and wrap the whole thing behind a custom defined Annotation.
I haven't yet developed a working solution but since I asked the questions, wanted to at least post something that may help others with the same sort of question, to get started. With all due respect, although this has all improved in Spring 3.2, it's still bit of a dogs dinner to get a little customization working... I really was expecting a one liner config change etc.
Rather than twist and bend Spring, perhaps the easiest answer for my particular issue is just to change JAXB2 implementation and use something like Eclipse Link JAXB that can do this out of the box.
Basically you need to create a custom HttpMessageConverter. Instead of relying on the Jaxb2RootElementHttpMessageConverter that spring uses by default.
Unfortunately, customizing one converter means you are telling spring that you will take care of loading all the converters you need! Which is fairly involved and can get complicated, based on whether you use annotations, component scanning, Spring 3.1 or earlier, etc.. The issue of how to add a custom converter is addressed here: Custom HttpMessageConverter with #ResponseBody to do Json things
In your custom message converter you are free to use any custom JAXB2 content handlers.
Another, simpler approach to solve your original problem would be to use a custom XmlJavaTypeAdapter. Create a custom implementation of javax.xml.bind.annotation.adapters.XmlAdapter to handle CDATA, in the marshal method wrap the return value with the cdata braces. Then in your mapped pojo, use the XmlAdapter annotation, pass it the class of your custom adapter and you should be done.
I have not myself implemented the adapter approach, so couldn't provide sample code. But it should work, and won't be a lot of work.
Hope this helps.

Terracotta - Cannot cast to com.tc.object.bytecode.TransparentAccess

I have a rather large spring application, and all I'm trying to share is a single Map (using util.ConcurrentMap as implementation).
To do this, I created a bean in my appContext, and I tried to use the following tc-config line:
*/applicationContext.xml
Must I do something else to enable this to work? MyClass is a rather simple domain object that contains only primitives, two constructors, and accessors/mutators.
Must I do something else to get this working? I'm using Terracotta 3.0.0.
You need to create a tc-config.xml config file as described in http://www.terracotta.org/web/display/orgsite/Spring+Integration.

Resources