# Autocomplete con findText

## Crear el índice

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

## Crear el Services

```java
/*
 * 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

```java
 <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>
```

![](/files/-Lc1X5r1mVw0WeDh6ALT)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/trucosjakartaee/overview/java-server-faces/autocomplete/autocomplete-con-findtext.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
