<< 使用Scrum的Agile项目管理介绍 | 首页 | 我收藏的链接(46) >>

在appengine上用compass来集成lucene实现全文搜索


using a compass + JDO Search on appengine

Information in 2.3.0-beta is.

How to use

  1. Download module
    1. http://build.compass-project.org/ of Compass Trunk of select Nightly Build.
    2. Results Page built "Artifacts" Click, "Release" to select.
    3. Likely to be a list of files, from which to download the compass-2.3.0-beta1.zip.
  2. Module Placement
    1. Unzip the downloaded module, add the following file CLASSPATH (Google Plugin If you use the "war / WEB-INF / lib" copy, Eclipse projects also add to the Build path.)
      1. commons-logging.jar
      2. compass-2.3.0-beta1.jar
      3. lucene-core.jar

Initialization

The demonstration video, PMF.java imitation because it was initialized with a static initializer.

Some PMF.java

private static final Compass compass;
 
private static final CompassGps compassGps;
 
static (
  compass = new CompassConfiguration ()
      . SetConnection ("gae: / / index")
      . SetSetting (
          CompassEnvironment.ExecutorManager.EXECUTOR_MANAGER_TYPE,
          "Disabled"). AddScan (
          "Jp.co.topgate.sandbox.compass / model"). BuildCompass ();
 
  compassGps = new SingleCompassGps (getCompass ());
  compassGps.addGpsDevice (new Jdo2GpsDevice ("appengine", INSTANCE));
  compassGps.start ();
  compassGps.index ();
)
 
public static Compass getCompass () (
  return compass;
)
 

addScan () does, JDO specifies the name of the Entity package that contains classes.
Entity Class
  • Class "@ Searchable" to qualify.
  • Entity can be used as the primary key field "@ SearchableId" to qualify.
    • However, "com.google.appengine.api.datastore.Key" is not bound to accept because, as I have below.
      @ SearchableId
      public Long getKeyValue () (
          return key.getId ();
      )
       
  • The search field to use as "@ SearchableProperty" to qualify.
    • However, "com.google.appengine.api.datastore.Text" is not bound to accept because, as I have below.
      @ SearchableProperty
      public String getContent1String () (
          if (content1 == null) (
              return "";
          )
          return content1.getValue ();
      )
       
  • @ SearchableProperty (name = "Field Name") in the class Entity and attribute names can I use different name too.
    • Also, org.apache.lucene.document.Field attributes that can be used when creating.

Search

Example of getting results

@ SuppressWarnings ("serial")
public static class SearchResult implements Serializable (
  public final String keyValue;
  public final String content;
  public final String nickname;
  public final String email;
 
  public SearchResult (Resource resource) (
    this.keyValue = resource.getId ();
    this.content = (String) resource.getProperty ("content1String"). getObjectValue ();
    this.nickname = (String) resource.getProperty ("nickname"). getObjectValue ();
    this.email = (String) resource.getProperty ("email"). getObjectValue ();
  )
)
 
private List <SearchResult> search (String keyWords) (
  CompassSearchSession search = PMF.getCompass (). OpenSearchSession ();
  CompassHits hits = search.find (keyWords);
  int length = hits.length ();
  List <SearchResult> result = new ArrayList <SearchResult> (length);
  for (int i = 0; i <length; i + +) (
    Resource resource = hits.resource (i);
    result.add (new SearchResult (resource));
  )
  return result;
)
 

If you want to search for the name Field

  • Name your search field: Search

If you specify a search for Kind

  • Search criteria alias: Kind Name (Class.getSimpleName ())

Resona

  • When you save through JDO, @ SearchableProperty were added using the getter and Field Index to create value, it seems to work.
    • Entity class, using annotations to Index If you do add to the information (that is what I wrote on this page), there always needs to be saved through JDO. LowLevelAPI saved and does not create Entity Index for the (natural or ....)
    • Unowned other Entity relationship holds a reference to its value after the Index can also fetch for it created. , But always return the value of persistence when the getter must return a value, so actually, the "fetch the referenced Entity before saving to keep you" need.

      unowned

      @ Persistence private Key otherEntityKey;
       
      @ NotPersistence private OtherEntity otherEntity;
       
      @ SearchablePropperty (name = "otherEntity")
      public Sting getOtherEntityValue () (
        return otherEntity! = null? otherEntity.getValue (): "";
      )
  • Entity Index for the creation, no one could get for a separate?
    • Once implemented the Task Queue Java platform, Index will be delayed for creating Entity can only be good ... and behavior.
      • Compass TaskQueue side and I may well be expected to support.
  • Want control over the creation of the Index
    • Without CompassGps, Compass.Getsearchengineindexmanager() call control.
      • Because some of the lucene subinterface for the smaller may be able to use it?
    • Entity is a separate index to Compass.Openindexsession() calls to control
  •  create dynamic query
    • Compass.queryBuilder () Get the querybuilder. 良Shinani later.
    • between, eq, ge, gt, le, le, and, or, like, wildcard and one with an array.
    •  CompassMultiPropertyQueryStringBuilder or search over multiple properties at once
    • CompassSearchSession.find (queryString) internally CompassSearchSession.queryBuilder (). QueryString (queryString). ToQuery (). Hits () have.
       
  • Add to sort search
  • limit, offset to add
    • I looked, was not sure.CompassHits todetach(int from, int size) so that, if this 1-2 times will reduce Data itself. I do not mean much.
  • Highlight the text you wish to search results
    • Search engines highlight text in general are presented as fragments around search terms that best match (for example, place a few hundred characters) lucene is called in other bestFragment.
    • The method to get this fragment CompassHits.highlighter (i). Fragment (propertyName) 

      To obtain fragments of the highlights

          CompassSearchSession searchSession = getCompass (). OpenSearchSession ();
          CompassHits hits = searchSession.find (text);

          int length = hits.getLength ();

          if (length> 0) (

           for (int i = 0; i <length; i + +) (

            / / Here are widespread PR highlights the string.
            String fragment = hits.highlighter (i). Fragment ("pr");
            Resource resource = hits.resource (i);
           )
          )
          return results;
    •  Wicket to display in IData highlighter in one form or another will take to get the fragment.

 

标签 : , , , ,



发表评论 发送引用通报