Results 1 to 4 of 4

Thread: Search on java with Compass

  1. #1
    Join Date
    Dec 2008
    Posts
    202

    Search on java with Compass

    Compass is an open source framework based on Lucene. The aim of Compass is to facilitate the difficult task of making a powerful and fast search engine using Java technology. The purpose of this post is to explain the operation of Compass through a simple example. Lucene is a framework that allows indexing the content of java objects and files to search on them. Compass has the functionality of Lucene but greatly simplifies development because it has some new features:
    • Offers a simplified API to work with top-level Lucene.
    • Mapping of objects using XML or annotations.
    • Transaction management.
    • Integration with ORMs (Hibernate and JPA) with Spring.
    • Can modify the objects indexed.
    • Distributed. Possibility of using the cloud to store the indexes. It integrates with GigaSpaces, Coherence and Terracotta.

  2. #2
    Join Date
    Dec 2008
    Posts
    202

    Re: Search on java with Compass

    Configuration

    Configuration is done via a XML file. In this example the configuration file will be src / compass.cfg.xml:

    Code:
    <Compass - core - configuration>
         <Compass>
             <! - Settings ->
             <Setting name = "compass.engine.connection">
                / Var / compass / example /
             </ Setting>
             <Setting name = "compass.engine.analyzer.default.type">
                snowball
             </ Setting>
             <Setting name = "compass.engine.analyzer.default.name">
                English
             </ Setting>
             <Prop key = "compass.engine.analyzer.english.stopwords">
                and, of, to, in, is, his, on I, with, for, her
             </ Prop>
             <! - Class mappings ->
             <Mapping class = "vro.examples.compass.model.NewsItem" />
         </ Compass>
     </ Compass - core - configuration>
    You will also have a series of words that do not take into account when analyzing the text (you can add the necessary terms to this list). It also indicates that you want to index objects of type NewsItem and therefore, will require taking into account the mapping annotations or XML configuration of this class.

    Annotations

    Compass To be indexed, map and look for a class to add a series of entries, or configure an XML file. This example will make book-entry because it is a little simpler:
    Code:
    @ Searchable public class NewsItem implements Serializable 
    (@ Searchable @ SearchableMetaData (name = "id") 
    private Long id; @ SearchableProperty (NullValue = "") 
    @ SearchableMetaData (name = "title") 
    private String title; @ SearchableProperty (NullValue = " ") 
    @ SearchableMetaData (name =" intro ") 
    private String intro; @ SearchableProperty (NullValue =" ") 
    @ SearchableMetaData (name =" content ") 
    private String content; 
    @ SearchableProperty (format =" yyyy-MM-dd ") 
    @ SearchableMetaData (name = "date") private Date date; ...
    We have used the following notations (see entries in the official documentation):
    • @ Searchable: the class must have this annotation to tell Compass that can work with it.
    • @ Searchable: indicates the unique identifier of the object.
    • @ SearchableProperty: indicates that you must index the contents of this variable in order to do research about it. You can configure its behavior, such as the value that we will index when the variable is null.
    • @ SearchableMetaData: name to be assigned to that variable in the Lucene index.

  3. #3
    Join Date
    Dec 2008
    Posts
    202

    Re: Search on java with Compass

    Indexing disk

    Indices for the indexing information can be stored on disk or database. Also available is the ability to store this information in a distributed fashion using distributed databases, cloud, etc. The example shown below uses disk storage, particularly in the path "/ var / compass / examples" of our system.

    Code:
    public class implements CompassServiceImpl CompassService 
    (private static Logger log = Logger. getLogger (CompassServiceImpl. class); 
    private Compass compass; public CompassServiceImpl () 
    (config = new CompassConfiguration CompassConfiguration (). configure ("/ compass.cfg.xml"); 
    compass = config. buildCompass ();) 
    .public void index (List list) throws CompassSession CompassException 
    (session = null; CompassTransaction tx = null; 
    try (session = compass. openSession () tx = session. BeginTransaction () 
    // if index isn't blocking if (! compass. getSearchEngineIndexManager (). isLocked ()) 
    (if (! compass. getSearchEngineIndexManager (). indexExists ()) (compass. getSearchEngineIndexManager (). createIndex ();) else (compass. getSearchEngineIndexManager () . cleanIndex ();) 
    for (T aux: list) (session. save (aux);) try (tx. commit ();) catch (CompassException ex) 
    (log. error ("Exception to commit transaction." + ex . toString ());)) else (throw new CompassException ("Index is blocked.");)) 
    catch (CompassException ex) (log. error ("Exception index." + ex. toString ());) 
    finally ( if (session! = null) (try (session. close ();) catch (CompassException ex) 
    (log. error ("Exception closing session." + ex. toString ());))))
    The index method (List list) indexes a list of objects of type T. Using generics to define the type and methods of service allows us to leverage the code when we will index car news account. If we hire only index would have to change the file src / compass.cfg.xml and define the mapping of the Car class, for example.

  4. #4
    Join Date
    Dec 2008
    Posts
    202

    Re: Search on java with Compass

    Search with pager

    Once the index can be searched easily. Wrapper for the results:

    Code:
    public class implements Serializable (ResultsWrapper
    
         totalResults private Integer;
         TotalPages private Integer;
         private List results;
         Long executionTime private;
         private List; paginator;
     ...
    Search method CompassServiceImpl class:

    Code:
    ResultsWrapper public search (String query, int page, int itemsPage) 
    throws pageCompass CompassException (int = 0; if (page> 0) 
    (pageCompass = page - 1;) CompassSearchHelper searchHelper = new CompassSearchHelper (compass, itemsPage) 
    results = searchHelper CompassSearchResults.search (new CompassSearchCommand (query, pageCompass)); 
    List itemList = new ArrayList (); 
    for (CompassHit hit: results. getHits ()) 
    (T item = (T) hit. getData (); itemList. add (item) ;) 
    ResultsWrapper ResultsWrapper wrapper = new () wrapper. setTotalResults (results. getTotalHits ()); 
    wrapper. setResults (itemList) if (results. getPage ()! = null & & results. getPage (). length> 0) ( wrapper. setTotalPages (results. getPage (). length); 
    List paginator = generatePaginator (results. getPage (). length, page); 
    wrapper. setPaginator (paginator);) return wrapper;
    Perform a search page on the Lucene index. An example of use would be to search "java conference" on a list of related news about the world java. Indexing listData given that (a) a list of news:
    Code:
     CompassService CompassServiceImpl compassService = new (); 
    compassService. Index (listData ());
    Search for the string "java conference":

    Code:
    Results = compassService ResultsWrapper.Search ("java conference", 1, 10);
      System. Out. Println (results);

Similar Threads

  1. Compass not working in HTC One V
    By Mithi in forum Portable Devices
    Replies: 3
    Last Post: 18-04-2012, 02:21 PM
  2. Does Blackberry Curve 9360 have a compass?
    By EdEddNEddy in forum Portable Devices
    Replies: 1
    Last Post: 01-04-2012, 04:20 PM
  3. Is digital compass present in all HTC HD2 ?
    By Whitfield in forum Portable Devices
    Replies: 5
    Last Post: 26-04-2011, 10:36 AM
  4. Compass movement to a text game (java)
    By juicer in forum Software Development
    Replies: 1
    Last Post: 08-04-2011, 12:09 AM
  5. E72 Compass extremely not accurate
    By kamina23 in forum Portable Devices
    Replies: 4
    Last Post: 21-04-2010, 10:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,011,350.16096 seconds with 16 queries