[WebLab-User] Créer un nouveau service web.

Nicolas Martin nico.aina at gmail.com
Wed Nov 2 11:17:32 CET 2011


Bonjour,

J'ai pour habitude d'utiliser deux méthodes :

la première qui construit la structure du ResultSet, puis la deuxième qui
construit les hits dans le ResultSet. Il faut les appeler l'une après
l'autre.

private ResultSet constructAndAnnotateResultSet(StringQuery squery, int
nbResults, int offset) {

        String uuid = UUID.randomUUID().toString();

        // Construit le ResultSet et le POK
        ResultSet resultSet = ResourceFactory.createResource("appoline",
"resultSet/" + uuid, ResultSet.class);
        PieceOfKnowledge resPOK =
ResourceFactory.createResource("appoline", "Pok/" + uuid,
PieceOfKnowledge.class);
        resultSet.setPok(resPOK);

        PoKHelper h = RDFHelperFactory.getPoKHelper(resPOK);
        h.setAutoCommitMode(false);

        // Construit les propiétés du ResultSet, du POK et de la Query
        h.createResStat(squery.getUri(), RDF.TYPE,
WebLab.getUriFromClass(StringQuery.class));
        h.createLitStat(resultSet.getUri(), IS_PRODUCED_BY,
this.getClass().getName());
        h.createResStat(resultSet.getUri(), RDF.TYPE,
WebLab.getUriFromClass(ResultSet.class));
        h.createResStat(resultSet.getUri(), IS_RESULT_OF, squery.getUri());
        h.createResStat(resultSet.getUri(), HAS_POK_RESULT,
resPOK.getUri());
        h.createResStat(resPOK.getUri(), IN_RESULT_SET_POK,
resultSet.getUri());
        h.createLitStat(resultSet.getUri(), HAS_EXPECTED_OFFSET, new
String(offset));

        h.commit();

        return resultSet;
    }


private ResultSet fillResultSetWithHits(ResultSet rs, List<HitContent>
hits, Query query, int nbRes) throws URISyntaxException, TwitterException {

        String uuid = UUID.randomUUID().toString();

        int i=0;
        for(HitContent hit : hits) {

            String huri = "weblab://appoline/hit/" + uuid + "/"+ i;

            // Creates hit properties
            h.createResStat(huri, RDF.TYPE,
RETRIEVAL_PROPERTY_NAMESPACE+"Hit");
            h.createLitStat(huri, WebLab.HAS_RANK, Integer.toString(i+1));
            h.createResStat(huri, IS_LINKED_TO_SOURCE,
person_twitter_account_uri);

            h.createLitStat(huri, LIT_PROPERTY1, hit.get1());
            h.createLitStat(huri, LIT_PROPERTY2, hit.get2());
            h.createResStat(huri, RES_PROPERTY1, hit.get3());

            i++;
        }

        h.createLitStat(rs.getUri(), HAS_NUMBER_OF_RESULTS, hits.size());
        h.commit();

        return rs;
    }


En espérant que cela puisse vous aider.

Nicolas.

2011/11/2 <jetsadabodin.pintong at st-cyr.terre-net.defense.gouv.fr>

> Bonjour ,
>
> J?ai essayé de créer un service Weblab. J?ai implémenté l?interface
> Searcher.
>
> Mon service a une méthode qui prendre (string,int,int,int,int,int) comme
> paramètres et il renvoie un List<AppolineObject> au retour.
>
> Je sais qu?il faut créer des Hits mais je n?arrive pas.
>
> Comment créer le ResultSet et les Hits pour mon code.
>
> Voici le code de « appolineSearch » :
>
>
> **
>
> *
>
> public List<AppolineObject> appolineSearch(String queryString, int
> nbfaults, int dist, int mode, int nbpage, int nbaffichage) throws
> IOException, ParseException {
>
>        System.out.println("debut");
>
> long start = 0;
>
> boolean error = false; //utilis? pour controler error messages
>
> String indexName = "/appoline/fulltext";//locale de fichiers index
>
> IndexSearcher searcher = null; //the searcher used to open/search the
> index
>
> org.apache.lucene.search.Query query = null; //Query sera cr? par the
> QueryParser ou Query builder
>
> TopDocs hits = null; //Resultat de recherche
>
> int startindex = 0; //the first index displayed on this page
>
> int maxpage = nbaffichage; //the maximum items displayed par page
>
> int thispage = 0;
>
>     List<AppolineObject> lstAppoline = new ArrayList<AppolineObject>();
>
> try {
>
> IndexReader reader = IndexReader.open(FSDirectory.open(new
> File(indexName)), true); // only searching, so read-only=true
>
> searcher = new IndexSearcher(reader); //create an indexSearcher for our
> page
>
> //NOTE: this operation is slow for large
>
> //indices (much slower than the search itself)
>
> //so you might want to keep an IndexSearcher
>
> //open
>
>   } catch (Exception e) { //any error that happens is probably due
>
> //to a permission problem or non-existant
>
> //or otherwise corrupt index
>
> System.out.println("error0: echec d'acceder aux fichiers d'indexation");
>
> error = true; //don't do anything up to the footer
>
> }
>
>     if (error == false) {
>
> System.out.println("approaching to indexing");
>
>       try {
>
>   startindex = nbpage;
>
>   } catch (Exception e) {
>
> System.out.println("error1");
>
> }
>
>          if (queryString == null) {
>
> System.out.println("noquery specified");
>
> }
>
>       try {
>
>       Session session =
> SessionFactoryUtil.getSession();//.getCurrentSession();
>
> FullTextSession fSession = Search.getFullTextSession(session);
>
> QueryBuilder qb =
> fSession.getSearchFactory().buildQueryBuilder().forEntity(Anomalie.class).get();
>
>   start = System.currentTimeMillis();
>
>  Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
>
>       /** mode de recherche
>
> 0: lucene normale
>
> 1: hibernate noramale
>
> 2:distance active
>
> 3:fuzzy
>
>  4:wildcard
>
> 5:dans un champ(semantique)**/
>
> switch (mode) {
>
>   case 0:
>
>   String[] tfields;
>
> tfields = new String[3];
>
> tfields[0] = "anoLibelle";
>
> tfields[1] = "anoSolutionUti";
>
> tfields[2] = "anoSolutionTech";
>
>   MultiFieldQueryParser mqp = new MultiFieldQueryParser(tfields,
> analyzer);
>
>   query = mqp.parse(queryString);
>
>   break;
>
>   case 1:
>
> query = qb.keyword().onFields("anoLibelle", "anoSolutionTech",
> "anoSolutionUti").matching(queryString).createQuery();
>
>     break;
>
> case 2:
>
> query =
> qb.phrase().withSlop(dist).onField("anoLibelle").andField("anoSolutionTech").andField("anoSolutionUti").sentence(queryString).createQuery();
>
>    break;
>
> case 3:
>
> query =
> qb.keyword().fuzzy().withPrefixLength(nbfaults).onField("anoLibelle").andField("anoSolutionTech").andField("anoSolutionUti").matching(queryString).createQuery();
>
>   break;
>
> case 4:
>
> query =
> qb.keyword().wildcard().onField("anoLibelle").andField("anoSolutionTech").andField("anoSolutionUti").matching(queryString).createQuery();
>
>   break;
>
> case 5:
>
> QueryParser qp = new QueryParser("anoId", analyzer);
>
> query = qp.parse(queryString);
>
> break;
>
> default:
>
> break;
>
> }
>
>           } catch (HibernateException e) {
>
> System.out.println("error2: echec de creer des querys");
>
> error = true;
>
> }
>
> }
>
>     if (error == false && searcher != null) {  // si on n'a pas d'erreur
>
>     thispage = maxpage; // default last element to maxpage
>
> hits = searcher.search(query, maxpage + startindex); // execute de query
>
>             if (hits.totalHits == 0) { // Si on trouve rien, on active
> mode 3
>
>   System.out.println("Aucun resultat est detect?. Fuzzy activ?");
>
>     error = true;
>
> }
>
> }
>
>     if (error == true) {//activation du mode 3
>
> for (int i = 1; hits.totalHits <= 10 && i < 4; i++) {
>
> Session session = SessionFactoryUtil.getSession();//.getCurrentSession();
>
> FullTextSession fSession = Search.getFullTextSession(session);
>
> QueryBuilder qb =
> fSession.getSearchFactory().buildQueryBuilder().forEntity(Anomalie.class).get();
>
> query =
> qb.keyword().fuzzy().withPrefixLength(1).onField("anoLibelle").andField("anoSolutionTech").andField("anoSolutionUti").matching(queryString).createQuery();
>
> thispage = maxpage;
>
> hits = searcher.search(query, maxpage + startindex);
>
> }
>
> if (hits.totalHits != 0) {
>
> System.out.println("Voice des des resultats fuzzi?s\n");
>
>  error = false;
>
> } else {
>
> System.out.println("Aucun resultat detect? changez le mot");
>
> }
>
> }
>
>     if (error == false && searcher != null) {
>
>         if ((startindex + maxpage) > hits.totalHits) {
>
> thispage = hits.totalHits - startindex; // set the max index to maxpage
> or last
>
> } // actual search result whichever is less
>
>     for (int i = startindex; i < (thispage + startindex); i++) { // for
> each element
>
> AppolineObject vlObject = new AppolineObject();
>
>   Document doc = searcher.doc(hits.scoreDocs[i].doc); //get the next
> document
>
>   String docanoId = doc.get("anoId"); //get its anoId
>
> String docanoLibelle = doc.get("anoLibelle"); //get its anoLibelle et
> ainsi suite
>
> String docanoSolutionTech = doc.get("anoSolutionTech");
>
> String docanoSolutionUti = doc.get("anoSolutionUti");
>
>   // then output!
>
>   vlObject.setAnoID(Integer.parseInt(docanoId));
>
> vlObject.setAnoLibelle(docanoLibelle);
>
>  vlObject.setAnoSolutionTech(docanoSolutionTech);
>
> vlObject.setAnoSolutionUtil(docanoSolutionUti);
>
> vlObject.setPertinence(hits.scoreDocs[i].score);
>
> lstAppoline.add(vlObject);
>
>       }
>
>   }
>
> if (searcher != null) {
>
> long elapsedTime = System.currentTimeMillis();
>
> System.out.println("debut : " + start);
>
> System.out.println("fin : " + elapsedTime);
>
> System.out.println("During: " + (elapsedTime - start));
>
> System.out.println("searcher marche");
>
> searcher.close();
>
>   }
>
>       return lstAppoline;
>
> }
>   *
> * *
>
> * *
>
> Cordialement
>
> SLT PINTONG Jetsadabodin.
>
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
>
> _______________________________________________
> User mailing list
> User at weblab-project.org
> http://weblab-project.org/mailman/listinfo/user_weblab-project.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://weblab-project.org/pipermail/user_weblab-project.org/attachments/20111102/18ae8bbc/attachment-0001.html>


More information about the User mailing list