Autocomplete con findText

Crear el índice

if(aulasFacade.createIndex(new Document("idaula","text"))){
           System.out.println("creo el indice");
       }else{
           System.out.println("no creo el indice");
       }

Crear el Services

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.ejbsiu.services;

import com.avbravo.avbravoutils.JsfUtil;
import com.avbravo.ejbsiu.ejb.AulasFacade;
import com.avbravo.ejbsiu.entity.Aulas;
import java.time.LocalTime;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.bson.Document;

/**
 *
 * @author avbravo
 */
@Stateless
public class AulasServices {
    @Inject
    AulasFacade aulasFacade;

    public static LocalTime getHour(){

       return LocalTime.now();
    }

    public List<Aulas> complete(String query) {
        List<Aulas> suggestions = new ArrayList<>();

        try {
            query = query.trim();
            if (query.length() < 1) {
                return suggestions;
            }

            suggestions = aulasRepository.findText("idaula",  query,new Document("idaula",1));

        } catch (Exception e) {
            JsfUtil.errorMessage("complete() " + e.getLocalizedMessage());
        }

        return suggestions;
    }


}

En el Controller

 @Inject
    AulasServices aulasServices;

     Aulas aulasSelected;


    public AulasServices getAulasServices() {
        return aulasServices;
    }

    public void setAulasServices(AulasServices aulasServices) {
        this.aulasServices = aulasServices;
    }

      public Aulas getAulasSelected() {
        return aulasSelected;
    }

    public void setAulasSelected(Aulas aulasSelected) {
        this.aulasSelected = aulasSelected;
    }
     @Override
    public void handleSelect(SelectEvent event) {
        try {

            aulasList.removeAll(aulasList);

            aulasList.add(aulasSelected);

            aulasFiltered = aulasList;

            aulasDataModel = new AulasDataModel(aulasList);

        } catch (Exception ex) {
            JsfUtil.errorMessage("handleSelect() " + ex.getLocalizedMessage());

        }

    }

En la página xhtml

 <div class="form-group row">
  <p:outputLabel class="col-xs-2 col-form-label" value="#{msg['info.by']} #{msg.idaula}"/>
  <div class="col-xs-4">
        <p:autoComplete  scrollHeight="250"   dropdown="false"  size="45"  
                  emptyMessage="#{msg['info.nohayregistros']}" 
                   title="#{msg['info.by']} #{msg.idaula}"
                   label="#{msg['info.by']} #{msg.idaula}"

                   alt="#{msg['info.searchby']} #{msg.idaula}"
                   value="#{aulasController.aulasSelected}"  
                   completeMethod="#{aulasController.aulasServices.complete}"  
                   var="p"  itemLabel="#{p.idaula}"  itemValue="#{p}" forceSelection="true"> 
                   <f:converter binding="#{aulasConverter}"/>
                   <f:attribute name="field" value="idaula}"/>
                   <f:attribute name="fielddropdown" value="false"/>
                  <f:attribute name="fieldquerylenth" value="1"/>
                  <p:ajax event="itemSelect" listener="#{aulasController.handleSelect}"
                          update=" :form:msgs,:form:dataTable" />  
                          <f:facet name="itemtip">
                            <h:panelGrid columns="1" cellpadding="5">
                               <h:outputText value="#{p.idaula}" />
                               <h:outputText value="#{p.maximo}" />
                           </h:panelGrid>
                          </f:facet>
          </p:autoComplete>   
  </div>

Last updated