5 Kasım 2015 Perşembe

Introduction to ElasticSearch - 101

Hello coderBros,

What I'm going to mention about is one of the top trending topics on search engineering world, ElasticSearch.

ElasticSearch uses Apache Lucene libraries on its backgorund which allows us to a very high performance, fast, easy to use and easy to implementation opportunities. I will not cover the adv and disadv of the engine. For ones who are interested, here a link is;

https://www.elastic.co/use-cases/stumbleupon

If you are a beginner on ElasticSearch and if you desire to work it on a JAVA project, all you need to do is just adding a few dependencies to your pom.xml of your maven project.

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.7.3</version>
</dependency>

<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-expressions</artifactId>
<version>4.10.2</version>
</dependency>

If you have any errror while you starting up your server something like:

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Socket read failed
Caused by: java.io.IOException: Socket read failed
All you need to just add two more dependency to pom.xml:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>

Then you can feel yourself free for a good starting after updating your maven project.


You can optionally ( Actually this is the best approach ) create yourself a bootstrap.java. The functionality of this bootstrap will be, as usually, while your server is up, your bootstrap will be read for once and will create a node and a client for you.

If you desire to launch your elasticSearch engine into your IDE, you can add following lines into your bootstrap;

Node node = NodeBuilder.nodeBuilder().node();
Client client = node.client();

This will directly creates you a node and a client to work on. Now you can start to index some data into your elasticSearch. After you create your first index, An extra folder will appear in your project hierarcy and will include your node and data information.

However,

If you want that your engine should work seperately than out of your IDE. All you need to do is installing current elasticSearch to your PC from here, then run it under bin/elasticsearch.bat.

Now your engine is up, congrats...

Turn back to your IDE and add following lines to your bootstrap.java 

Settings settings = ImmutableSettings.settingsBuilder()
       .put("cluster.name", "cluster.value").build();
transportClient = new TransportClient(settings);
TransportClient client = transportClient.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",9300));

Those lines will direct your comments to your seperate elasticSearch engine.

To remind you guys, you should have your elasticsearch.yml  file created on \src\main\resources\elasticsearch.yml. 

There is no configuration needed to make this file read. Just create a client or a transport client. While creation process it looks under your \src\main\resources\ if there is a config file.

In your elasticsearch.yml file the lines should be:

cluster.name: myCluster-ES
node.name: myNode-01
node.client: true
network.host: 127.0.0.1

After these little code blocks now you are ready to index. But don't forget to stop your client and node after your work is done.

client.close();
node.close();

Have Fun ...










12 Şubat 2013 Salı

What is 'Spring Framework' and Hierarchy for Best Use of it

Hi everyone,

    This is the topic about a stable framework called Spring that allows us to develop software especially by using JAVA language. Indeed, Spring Framework was developed as a software developing kit for working on JAVA and .NET. With the help of its extra components like (Spring Web Flow or Spring Framework itself etc.) web pages can be developed using JAVA Enterprise platform. Among other frameworks, Spring has come a step forward to become an alternative EJB(Enterprise JavaBean) model.

    The ones who want to develop a software using Spring Framework must implement the Spring needed components to their projects. Either, while you are creating a new project, you can select Spring from the desired technologies part of the window, or, after creating an empty project, you can make all needed configurations by yourself manually - which I don't really want to cope with -.

    Here is the screenshot of the project start up window. I feel myself comfortable with using IntellijIDEA 11.1.3 for IDE and my Spring Framework version is 3.1.1. These version information are 'must be given' data for a blogger. Because I had lots of troubles relevant to version differences of Spring itself, Spring Security and Spring Webflow and whenever I checked any blogs or any other Spring tutorials, they do the same solutions as mine, however mine was not resolved because of the version differences. This is a mandatory check for all the programmers if they don't want to get an headache. Use latest and stable versions of the technologies you prefer to use.



    Spring Framework is an easy to use technology once after you learnt it. In order to make learning process less stressful, you should work clean as I mentioned in my other blog topics. 'Working clean' term is based on 4 main components.

1) Controller,
2) Form,
3) Validator,
4) JSP file.

    All these components must be collected under seperate main classes in order to find them easily, or, if your project contains too many different features, you can put all these components (not including the JSP file, it should be under another file called 'jsp') under the name of that feature.


    Here as you see my related java classes are in one part (above one), and other related jsp files are under the jsp file(below one). I divide my project structure feature by feature, since its a big one, so it's easy to find for me what I'm searching for.


1. Controller

    Controller is the key part of the hierarchy which is a bridge between the view (JSP file) and the backend(server-side) of the project. You manage all the operations at the backend side and return the result to controller and preferentially you can put the result to a map, to the HttpSession, or in a model and put the result in a ModelandView object and return it to ModelandView object. After that you can call the related value from JSP file.

@RequestMapping(value = "/pri/SuperUser/ResetPassword.qib", 
method = RequestMethod.POST)
public ModelAndView submit(@ModelAttribute("resetPasswordForm") ResetPasswordForm   resetPasswordForm,
                               BindingResult bindingResult,
                               HttpSession session,
                               Model model){

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

        map.put("response1", "Here can be a object comes from backend side");

        session.setAttribute("response2", "Here can be a object comes from backend side");

        return new ModelAndView("/pri/SuperUser/ResetPassword", map);
    }

    This code piece is from my Controller. As I mentioned above I can put my result in a map or in a session to view it later on my JSP. No need to return session because it derives from HttpSession which will always be written to session without any return value.However the map you created must be returned via ModelandView Object.

    I want to take your attention to the very begining part of my code. There is a @RequestMapping attribute there. Well, here is the Spring magic! Spring communicate with itself by using these '@' tags. You put these '@'s almost everywhere of your project as long as you want Spring to read the codes you wrote. Here, RequestMapping attribute takes the requests to this URL (within the 'value' tag) and progresses it.

    The thing that you shouldn't forget before start to write your Controller class is, you should always write @Controller before your Controller class starts otherwise your Controller class will never be seen and the requests that you made will not be delivered to @RequestMapping attribute;


@Controller
public class ResetPasswordController extends SomeNeededClassesQueue {
...
...
...
}

    The final issue about the Controller component is the return value of @RequestMapping related class. You can either return String or Modelandview object. However in both cases, you should certainly be giving the path of your JSP file that you want to proceed/show the info from your requestmapping method.

return "redirect:/pri/SuperUser/ResetPassword";

or

 return new ModelAndView("/pri/SuperUser/ResetPassword", map);

    If you want to return a series of information, you should put them in a map, which means you should return to ModelandView. Actually you can still return to String, but therefore you should but this map in a session or in a model which we don't want to use these because of the security reasons. You must not put everyting in a session. Model also puts things in a session, but just for that page, but again, it's a security failure.

    By the way, the method name for your requestmapping has no importance. Even you can put "aasdfasdf" :) You never call it by its method name.


...to be continued under this topic

Ajax usage using JavaScript (JQuery)


Hi guys,

    As far as I step into new projects, I will try to inform all of you with new algorithms, syntaxes and some recent post-modern technological tools/developments/usages of plug-ins.

    Nowadays, my project includes some front-end specilities which focuses on dynamic data transaction. The proejct is about an Admin Panel makes some automation for the end-users and includes the duty of controlling them. Because of the fact that we want to get the datas of the users and their related information without refleshing the browser page, AJAX ( Asyncronous JavaScript And XML ) is our best option to view a page. Instead of not wasting our time and energy for creating visual effects, JQuery library is used in this project with the advantage of its developer-friendly "$.ajax" attribute.

( JQuery web page: http://jquery.com/ )
( A good example of JQuery AJAX usage using JSP: http://www.mobisoftinfotech.com/blog/web-programming/jquery-datatable-ajax-tutorial-with-example-project/ )

For general knowledge, what a developer must to is performing a clean work. What is 'a clean work' then? Using a clean hierarchical structure, creating folders with its related sub-contents, using object oriented structure while developing process in the code, creating generic libraries, methods in order not to write/implement same lines of codes over and over again at the different part of the project. In other words, for this project we can divide it into two seperate parts. One is for the visualized part which includes html files in it, other part is for javascript files for each pages. Thus, we don't need to write thousands of javascript codes into our html files. We can call all the javascript files like;

<!--CSS Files-->
<link rel="stylesheet" href="css/master.css">
<link rel="stylesheet" href="css/tables.css">
<link rel="stylesheet" href="css/iphone-check.css">

<!--JS Files-->
<script src="js/min/jquery-1.8.3.min.js"></script>
<script src="js/min/jquery-ui-1.9.2.min.js"></script>
<script src="js/min/jquery.dataTables-1.9.4.min.js"></script>
<script src="js/min/sticky.min.js"></script>
<script src="js/min/forms/iphone-check.min.js"></script>
<script src="js/min/global.min.js"></script>
<script src="js/min/auth.min.js"></script>
<script src="js/min/pins.min.js"></script>


<!--Fonts-->
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>

...to be continued