Showing posts with label ubuntu. Show all posts
Showing posts with label ubuntu. Show all posts

Saturday, 26 January 2013

1) Open Terminal and first become root user:

sudo su

2) Now we need to download the package sphinxsearch 2.0.5 from the Sphinx Search site.

wget http://sphinxsearch.com/files/sphinxsearch_2.0.5-release-0ubuntu11~precise2_amd64.deb

3) Sphinx Search 2.0.5 requires a dependency package called libpq5 that you might not have yet on your server so run the following to install the deb package:

# install dependency  
apt-get install libpq5  
 
# install deb package  
dpkg -i sphinxsearch_2.0.5-release-0ubuntu11~precise2_amd64.deb

NOTE: Now we have Sphinx Search 2.0.5 installed on our Ubuntu Server installation and we are ready to index some of our mysql data. Here are the programs we need to get the job done: (don’t run this commands)

/usr/bin/searchd  
/usr/bin/indexer  
/usr/bin/search

For example purpose, we have a folder structure of

/home/indianic/web/sphinx/etc
/home/indianic/web/logs

4) The above line shows that we have an etc in a sphinx directory which is were we store our config file

cat /home/indianic/web/sphinx/etc/sphinx.conf

Below is our configuration file that Sphinx Search will use to index the data we want indexed. Our purpose is to allow full text search so the configuration below covers what such a setup might look like.

    source indianiccom  
    {  
       type                            = mysql      
       sql_host                        = localhost  
       sql_user                        = root  
       sql_pass                        = root  
       sql_db                          = mysqldatabase_sphinx  
       sql_port                        = 3306  
     
       sql_query_range = SELECT MIN(id), MAX(id) FROM posts  
       sql_range_step  = 128  
       sql_query       = SELECT id, created, modified, title, content, tags, short_description, author_id FROM posts WHERE id>=$start AND id<=$end  
    }  
     
    index indianiccom {  
       source = indianiccom  
       path = /home/indianic/web/sphinx/sphinx  
       morphology = stem_en  
       min_word_len = 3  
       min_prefix_len = 0  
    }  
     
    searchd {  
       compat_sphinxql_magics = 0  
       port = 3313  
       log = /home/indianic/web/logs/searchd.log  
       query_log = /home/indianic/web/logs/query.log  
       pid_file = /home/indianic/web/logs/searchd.pid  
       max_matches = 10000  
    }

5) Now that our configuration file is ready and points out exactly what we want sphinxsearch to index, we can move on to the actual indexing. Still as root user do the following to index your data:

/usr/bin/indexer --config /home/indianic/web/sphinx/etc/sphinx.conf --all

6) Now your mysql query in the config file is indexed. We can now move forward and set up sphinxsearch to start at startup of our server in case we need to reboot or something happens that restarts your server. As root do the following:

nano /etc/rc.local

7) Now we need to add the following command right before the last line which displays exit 0 so that our file looks like:

#!/bin/sh -e  
#  
# rc.local  
#  
# This script is executed at the end of each multiuser runlevel.  
# Make sure that the script will "exit 0" on success or any other  
# value on error.  
#  
# In order to enable or disable this script just change the execution  
# bits.  
#  
# By default this script does nothing.  
 
/usr/bin/searchd --config /home/indianic/web/sphinx/etc/sphinx.config  

exit 0

8) Make sure to replace indianic with your directory. What the above script does is it starts the search daemon using the configuration file we used earlier.
Now let us test our installation by starting the sphinxsearch daemon. Run the following to start searchd:

/usr/bin/searchd --config /home/indianic/web/sphinx/etc/sphinx.conf

9) Once the we have searchd running we can test if the index work by doing the following:
eg: /usr/bin/search -c /home/indianic/web/sphinx/etc/sphinx.conf <search key>

/usr/bin/search -c /home/indianic/web/sphinx/etc/sphinx.conf mysql

10) The result should look similar to what I have below. Obviously if your data has different content yours would look different but the below display is just so you can see how it is supposed to look like.

Sphinx 2.0.5-id64-release (r3308)  
Copyright (c) 2001-2012, Andrew Aksyonoff  
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)  
 
using config file '/home/indianic/web/sphinx/etc/sphinx.conf'...  
index 'indianiccom': query 'mysql ': returned 15 matches of 15 total in 0.024 sec  
 
displaying matches:  
1. document=120, weight=4660  
2. document=125, weight=4655  
3. document=6, weight=4645  
4. document=115, weight=4645  
5. document=100, weight=4634  
6. document=93, weight=3660  
7. document=99, weight=3645  
8. document=60, weight=2609  
9. document=118, weight=1645  
10. document=105, weight=1634  
11. document=10, weight=1624  
12. document=7, weight=1609  
13. document=117, weight=1609  
14. document=119, weight=1609  
15. document=108, weight=1579  
 
words:  
1. 'mysql': 15 documents, 82 hits

11) When you want to index the mysql’s updated data, then you have to run the following command:

/usr/bin/indexer --rotate --config /home/indianic/web/sphinx/etc/sphinx.conf --all

12) In Java:

a) Go to the path where Sphinx has been installed:
cd /usr/share/sphinxsearch/api/java

Creating sphinxapi.jar file:
make

The above “make” command will create sphinxapi.jar file.
   
    b) Create project in eclipse and add this sphinxapi.jar in classpath.
    c) Create a new class SphinxMain.java as follow:

    package com.demo;

import java.util.Date;

import org.sphx.api.SphinxClient;
import org.sphx.api.SphinxException;
import org.sphx.api.SphinxMatch;
import org.sphx.api.SphinxResult;
import org.sphx.api.SphinxWordInfo;
public class SphinxMain {
   public SphinxClient client = new SphinxClient();
   public String index = "*" ;
   
   public SphinxMain() {
       try {
           client.SetServer("localhost", 3313);
       } catch (SphinxException e) {
           e.printStackTrace();
       }
   }
   
   public static void main(String[] args) {
       SphinxMain object = new SphinxMain();
       object.searchValue("sunil");
       object.searchValue("tag");
       object.searchValue("asdasdasdasd");
      }
   public void searchValue(String query)
   {
       System.out.println("***********************************************");
       System.out.println("Search Key: " + query);
       System.out.println("***********************************************");
       try {
           SphinxResult res = client.Query(query, index);
           if ( res==null )
           {
               System.err.println ( "Error: " + client.GetLastError() );
               System.exit ( 1 );
           }else
           {
               printResult(query, res);
           }
       } catch (SphinxException e) {
           e.printStackTrace();
       }        
   }
   
   public void printResult(String query,SphinxResult res)
   {
       /* print me out */
       System.out.println ( "Query '" + query + "' retrieved " + res.total + " of " + res.totalFound + " matches in " + res.time + " sec." );
       System.out.println ( "Query stats:" );
       for ( int i=0; i<res.words.length; i++ )
       {
           SphinxWordInfo wordInfo = res.words[i];
           System.out.println ( "\t'" + wordInfo.word + "' found " + wordInfo.hits + " times in " + wordInfo.docs + " documents" );
       }

       System.out.println ( "\nMatches:" );
       for ( int i=0; i<res.matches.length; i++ )
       {
           SphinxMatch info = res.matches[i];
           System.out.print ( (i+1) + ". id=" + info.docId + ", weight=" + info.weight );
           if ( res.attrNames==null || res.attrTypes==null )
               continue;

           for ( int a=0; a<res.attrNames.length; a++ )
           {
               System.out.print ( ", " + res.attrNames[a] + "=" );

               if ( res.attrTypes[a]==SphinxClient.SPH_ATTR_MULTI || res.attrTypes[a]==SphinxClient.SPH_ATTR_MULTI64 )
               {
                   System.out.print ( "(" );
                   long[] attrM = (long[]) info.attrValues.get(a);
                   if ( attrM!=null )
                       for ( int j=0; j<attrM.length; j++ )
                   {
                       if ( j!=0 )
                           System.out.print ( "," );
                       System.out.print ( attrM[j] );
                   }
                   System.out.print ( ")" );

               } else
               {
                   switch ( res.attrTypes[a] )
                   {
                       case SphinxClient.SPH_ATTR_INTEGER:
                       case SphinxClient.SPH_ATTR_ORDINAL:
                       case SphinxClient.SPH_ATTR_FLOAT:
                       case SphinxClient.SPH_ATTR_BIGINT:
                       case SphinxClient.SPH_ATTR_STRING:
                           /* ints, longs, floats, strings.. print as is */
                           System.out.print ( info.attrValues.get(a) );
                           break;

                       case SphinxClient.SPH_ATTR_TIMESTAMP:
                           Long iStamp = (Long) info.attrValues.get(a);
                           Date date = new Date ( iStamp.longValue()*1000 );
                           System.out.print ( date.toString() );
                           break;

                       default:
                           System.out.print ( "(unknown-attr-type=" + res.attrTypes[a] + ")" );
                   }
               }
           }

           System.out.println();
       }
   }
   
}




Open Terminal and Execute following commands:

-------------------------------------------------------- RHC SETUP ------------------------------------------------------
STEP 1) Install Redhat Packages:

$ sudo gem install rhc

STEP 2) Setup Redhat:

$ rhc setup

STEP 3) Check the setup is properly done:

rhc domain show OR rhc app show --state

---------------------------------------------- RHC SETUP COMPLETED ----------------------------------------------



--------------------------------------------------------- EXAMPLE-----------------------------------------------------------

STEP 4) Create an application using rhc command where ever you want. Note that according to the value of the local git repository path you declare, this is where  your application will be created. This will create a project with all folder structure and other web app descriptor files.
So for example if you use the following rhc command:

$ rhc-create-app -a HelloWorld -l sunil.gulabani@indianic.com -t jbossas-7 -r HelloWorld

you will see that HelloWorld directory created at the path that was supplied with -r created. See below the content of HelloWorld directory after creation:


STEP 5) Now with maven you can easily turn this application into an eclipse application.
Switch your working directory to the folder of you new application for example

$ cd HelloWorld

Now use the following maven command in order to prepare the project for import to eclipse:

$ mvn eclipse:eclipse

Once the maven task end the project is ready to be imported into eclipse.

STEP 6) Before starting adding content removing the current pom.xml and other artifacts created at application creation is required:

$ git rm -r src pom.xml

$ git commit -m "removing artifacts from application creation phase"



STEP 7) Import the project to eclipse
We start by importing an existing project into eclipse

Now select the directory of you new application as the root:

Click next and the project will appear in you eclipse projects tree.



STEP 8) Change the project facet to Dynamic Web, Java (1.6 and above), Javascript

Changing the project facet is just a matter of few clicks:
- Right click on your project and select properties.
- Select Project Facets (press convert to allowing facets to be defined for you project as eclipse suggests).

- Click ok. You project now is configured in eclipse as Dynamic Web Project which is more convenient for later common web app tasks.

- Now lets add a simple jsp file to the project before commiting and deploying to openshift express.

- Just right click on the project and select New–>jspFile (as the project facet is dynamic web app new jsp is available and will be placed under the WebContent folder the same will be available with other web resources like servlets).
    - Now its time to commit and deploy to openshift.

STEP 9) Creating a war file The easiest way to package the project into a war via eclipse export option so just right click on the project and export as HelloWorld.war into the HelloWorld/deployments folder of the application.

STEP 10) From console: Add to your openshift git commit and push
Now that you have a war file ready for deployment (HelloWorld/deployments/HelloWorld.war) lets add it to the git repository and then commit and finally we will push the war to the openshift express instance.
Open a terminal window and switch the working directory to the application’s deployments directory then execute the following commands:

$ cd deployments/

$ git add HelloWorld.war

$  git commit -m "depolying myapp application" HelloWorld.war

$ git push

Note that git repository is created at the root project level so all files can be added to git and not only the deployment.

STEP 11) Your push completed and you application should be available at the following openshift URL:

http://helloworld-jsr10.rhcloud.com/HelloWorld/index.jsp
Find me on Facebook! Follow me on Twitter!