Example usage for org.apache.lucene.analysis.es SpanishAnalyzer SpanishAnalyzer

List of usage examples for org.apache.lucene.analysis.es SpanishAnalyzer SpanishAnalyzer

Introduction

In this page you can find the example usage for org.apache.lucene.analysis.es SpanishAnalyzer SpanishAnalyzer.

Prototype

public SpanishAnalyzer(CharArraySet stopwords) 

Source Link

Document

Builds an analyzer with the given stop words.

Usage

From source file:buscador.IndexFiles.java

License:Apache License

/**
 * Index all text files under a directory.
 *//*from  w  w  w . j a  v a 2  s  . c  om*/
public static void main(String[] args) {
    String usage = "java org.apache.lucene.demo.IndexFiles"
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles";
    String indexPath = "Zaguan1";
    String docsPath = null;
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];
            i++;
        } else if ("-docs".equals(args[i])) {
            docsPath = args[i + 1];
            i++;
        } else if ("-update".equals(args[i])) {
            create = false;
        }
    }

    if (docsPath == null) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(new File(indexPath));
        Analyzer analyzer = new SpanishAnalyzer(Version.LUCENE_44);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);
        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);
        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:buscador.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/*w  w  w.j  a  v  a2s. com*/
    }

    String index = "Zaguan1";
    String[] fields = { "title", "description", "identifier", "date", "creator" };
    BooleanClause.Occur[] flags = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };

    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;

        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new SpanishAnalyzer(Version.LATEST);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }

    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

        if (line == null || line.length() == -1) {
            break;
        }

        line = line.trim();
        if (line.length() == 0) {
            break;
        }

        Query query = MultiFieldQueryParser.parse(line, fields, flags, analyzer);

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:cl.usach.escalemania.sessionbeans.DocumentoFacade.java

@Override
public List<Documento> buscarDocumento(String frase, List<Documento> documentos) {

    @SuppressWarnings("deprecation")
    SpanishAnalyzer analyzer = new SpanishAnalyzer(Version.LUCENE_4_10_1);
    //System.out.print(tokenizeString(analyzer, "certificado obra municipales"));
    List<String> palabras = tokenizeString(analyzer, frase);
    int cantidadPalabras = palabras.size();
    List<List<Documento>> resultados = new ArrayList<>();
    for (int i = 0; i < cantidadPalabras; i++)
        resultados.add(new ArrayList<Documento>());

    List<String> textoAnalizar;
    int ocurrencia = 0;

    for (Documento doc : documentos) {
        textoAnalizar = tokenizeString(analyzer, doc.getNombre());
        for (String palabra : palabras) {
            if (textoAnalizar.contains(palabra))
                ocurrencia++;//from  w  ww  .j  av a 2  s .  c o  m
        }
        if (ocurrencia != 0)
            resultados.get(ocurrencia - 1).add(doc);
        ocurrencia = 0;
    }
    List<Documento> resultadoDocumentos = new ArrayList<>();
    for (int i = cantidadPalabras - 1; i >= 0; i--)
        resultadoDocumentos.addAll(resultados.get(i));
    return resultadoDocumentos;
}

From source file:de.innovationgate.wgpublisher.lucene.LuceneManager.java

License:Open Source License

/**
 * wga-configuration has changed, read new configuration and do necessary index updates
 *///from  w  ww  .ja  v a  2 s .c  o m
public synchronized void configurationHasChanged(Set newConnectedDBKeys) {
    if (!_started) {
        // skip config update if lucene manager has not been started (method startup called) yet
        // this happens on an initial WGA startup
        return;
    }

    if (_core.getWgaConfiguration().getLuceneManagerConfiguration().isUseLanguageAnalyzers()) {
        _core.addAnalyzerMapping("de", new GermanAnalyzer(org.apache.lucene.util.Version.LUCENE_35));
        _core.addAnalyzerMapping("en", new EnglishAnalyzer(org.apache.lucene.util.Version.LUCENE_35));
        _core.addAnalyzerMapping("it", new ItalianAnalyzer(org.apache.lucene.util.Version.LUCENE_35));
        _core.addAnalyzerMapping("fr", new FrenchAnalyzer(org.apache.lucene.util.Version.LUCENE_35));
        _core.addAnalyzerMapping("es", new SpanishAnalyzer(org.apache.lucene.util.Version.LUCENE_35));
    } else {
        _core.removeAllAnalyzerMappings();
    }

    _indexReleasedOnly = _core.getWgaConfiguration().getLuceneManagerConfiguration()
            .isIndexReleasedContentsOnly();

    // check if each DB in _indexedDBKeys is in configfile and enabled by wga
    // if not create dropRequest
    Iterator itIndexedDbKeys = _indexedDbs.keySet().iterator();
    while (itIndexedDbKeys.hasNext()) {
        String dbKey = (String) itIndexedDbKeys.next();

        ContentStore dbConfig = _core.getWgaConfiguration().getContentStore(dbKey);
        if (dbConfig == null) {
            // indexed db not found in config, remove db and drop from index
            removeDatabase(dbKey, true);
            // remove from indexed dbs, cannot be done in removeDatabase() because of current iteration over indexedDbKeys
            //itIndexedDbKeys.remove(); // now done in removeDatabase via copy-replace
        } else if (!dbConfig.isEnabled()) {
            // if db was disabled, only remove from indexedDbs - do not drop index
            //itIndexedDbKeys.remove();
            removeDatabase(dbKey, false);
        }
    }

    // get all active databases from core
    Iterator contentDbs = _core.getContentdbs().values().iterator();
    while (contentDbs.hasNext()) {
        WGDatabase db = (WGDatabase) contentDbs.next();
        // use db only if it is a real contentStore (has feature FULLCONTENTFEATURES)
        if ((db != null) && (db.hasFeature(WGDatabase.FEATURE_FULLCONTENTFEATURES))) {

            // WGA Plugins are not fulltext indexed
            if (db.getDbReference().startsWith(PluginConfig.PLUGIN_DBKEY_PREFIX)) {
                continue;
            }

            // If db not yet connected, listen for connect event and execute this method again when it happens
            if (!db.isConnected()) {
                db.addDatabaseConnectListener(this);
                continue;
            }

            createOrUpdateDBIndex(db, newConnectedDBKeys);
        }
    }

}

From source file:fr.lipn.yasemir.Yasemir.java

License:Open Source License

/**
 * Initialisation method to be called before every action
 * @param configFile// www . j a  va 2 s  .  c  o  m
 */
public static void init(String configFile) {
    System.err.println("Reading config file...");
    ConfigurationHandler.init(configFile);

    //setting paths
    YASEMIR_HOME = ConfigurationHandler.YASEMIR_HOME;
    INDEX_DIR = YASEMIR_HOME + System.getProperty("file.separator") + ConfigurationHandler.INDEXDIR;
    TERM_DIR = YASEMIR_HOME + System.getProperty("file.separator") + ConfigurationHandler.TERMIDXDIR;
    //TERM_DIR=INDEX_DIR+System.getProperty("file.separator")+ConfigurationHandler.TERMIDXDIR;
    COLLECTION_DIR = ConfigurationHandler.CORPUSDIR;
    idField = ConfigurationHandler.DOCIDFIELD;
    ID_ASATTR = ConfigurationHandler.IDFIELD_ASATTR;
    DOC_DELIM = ConfigurationHandler.DOC_DELIM;
    COLLECTION_LANG = ConfigurationHandler.CORPUSLANG;

    if (COLLECTION_LANG.equals("fr"))
        analyzer = new FrenchAnalyzer(Version.LUCENE_44);
    else if (COLLECTION_LANG.equals("it"))
        analyzer = new ItalianAnalyzer(Version.LUCENE_44);
    else if (COLLECTION_LANG.equals("es"))
        analyzer = new SpanishAnalyzer(Version.LUCENE_44);
    else if (COLLECTION_LANG.equals("de"))
        analyzer = new GermanAnalyzer(Version.LUCENE_44);
    else if (COLLECTION_LANG.equals("pt"))
        analyzer = new PortugueseAnalyzer(Version.LUCENE_44);
    else if (COLLECTION_LANG.equals("ca"))
        analyzer = new CatalanAnalyzer(Version.LUCENE_44);
    else if (COLLECTION_LANG.equals("nl"))
        analyzer = new DutchAnalyzer(Version.LUCENE_44);
    else if (COLLECTION_LANG.equals("ar"))
        analyzer = new ArabicAnalyzer(Version.LUCENE_44);
    else
        analyzer = new EnglishAnalyzer(Version.LUCENE_44);

    //setting search mode
    String sm = ConfigurationHandler.SEARCH_MODE;
    if (sm != null) {
        if (sm.equalsIgnoreCase("semantic"))
            MODE = SEMANTIC;
        else if (sm.equalsIgnoreCase("hybrid"))
            MODE = HYBRID;
        else
            MODE = CLASSIC;
    }

    //setting concept similarity measure
    String smm = ConfigurationHandler.SIM_MEASURE;
    if (smm != null) {
        if (smm.equalsIgnoreCase("pg1"))
            SIM_MEASURE = ConceptSimilarity.PROXYGENEA1;
        else if (smm.equalsIgnoreCase("pg2"))
            SIM_MEASURE = ConceptSimilarity.PROXYGENEA2;
        else if (smm.equalsIgnoreCase("pg3"))
            SIM_MEASURE = ConceptSimilarity.PROXYGENEA3;
        else
            SIM_MEASURE = ConceptSimilarity.WU;
    }

    //setting concept weights
    String cw = ConfigurationHandler.CONCEPTWEIGHT;
    if (cw != null) {
        if (cw.equalsIgnoreCase("fixed"))
            CONCEPT_WEIGHTS = ClassWeightHandler.FIXED;
        else if (cw.equalsIgnoreCase("idf"))
            CONCEPT_WEIGHTS = ClassWeightHandler.IDF;
        else if (cw.equalsIgnoreCase("prob"))
            CONCEPT_WEIGHTS = ClassWeightHandler.PROB;
        else if (cw.equalsIgnoreCase("gauss"))
            CONCEPT_WEIGHTS = ClassWeightHandler.GAUSSPROB;
    }

    //setting annotator
    ANNOTATOR = ConfigurationHandler.ANNOTENGINE;
    annotator = new SentenceBasedAnnotator(TERM_DIR);
    //annotator=new KNNAnnotator(TERM_DIR); //TODO: not finished (select annotator depending on configuration file)
    try {
        Class<?> cls = Class.forName(ANNOTATOR);
        Constructor<?> constructor = cls.getConstructor(String.class);
        annotator = (SemanticAnnotator) constructor.newInstance(TERM_DIR);
        //Object instance = constructor.newInstance("stringparam");
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(
                "[YaSemIR]: failed to load the specified annotator, falling back to IndexBasedAnnotator");
        annotator = annotator = new SentenceBasedAnnotator(TERM_DIR);
    }
    //setting ngrams enabled or not
    CKPD_ENABLED = ConfigurationHandler.NGRAMS_ENABLED;

    //setting semantic fields
    semBalises = new HashSet<String>();
    semBalises.addAll(ConfigurationHandler.getSemanticFields());

    //setting classic fields
    clsBalises = new HashSet<String>();
    clsBalises.addAll(ConfigurationHandler.getClassicFields());

    //setting score type
    SCORE = ConfigurationHandler.SCORE;

    //setting ontologies and terminologies
    System.err.println("[YaSemIR]: Loading Knowledge Battery...");

    HashMap<String, String> ontoSKOSconf = ConfigurationHandler.getOntologySKOSMap();
    HashMap<String, String> ontoRootconf = ConfigurationHandler.getOntologyRootMap();

    for (String ontoLoc : ontoSKOSconf.keySet()) {
        String ontoRoot = ontoRootconf.get(ontoLoc);
        Ontology o = null;
        if (ontoRoot.trim().isEmpty())
            o = new Ontology(ontoLoc);
        else
            o = new Ontology(ontoLoc, ontoRoot);
        System.err.println("[YaSemIR]: loaded ontology: " + o.getBaseAddr() + " at " + ontoLoc);
        String termPath = ontoSKOSconf.get(ontoLoc);
        SKOSTerminology t = null;
        if (!termPath.trim().isEmpty()) {
            System.err.println("[YaSemIR]: loading terminology from " + termPath);
            t = new SKOSTerminology(o.getOntologyID(), termPath);
        } else {
            System.err.println("[YaSemIR]: no terminology provided: generating trivial terminology from "
                    + o.getBaseAddr() + "...");
            t = o.generateTerminology();
        }
        System.err.println("[YaSemIR]: loaded terminology: " + t.getTerminologyID());
        KnowledgeBattery.addOntology(o, t);

    }
    if (INDEXING_MODE)
        KnowledgeBattery.createTermIndex();
    System.err.println("[YaSemIR]: Done.");

}

From source file:it.unipd.dei.ims.lucene.clef.AnalyzerFactory.java

License:Apache License

public static Analyzer createAnalyzer(String language, String stemmer, CharArraySet stopset) {

    Analyzer analyzer;//  ww  w  .  java  2  s . c om

    if (stemmer.equalsIgnoreCase("NONE")) {

        analyzer = new StandardAnalyzer(stopset);

    } else { // otherwise use language-specific analyzer

        switch (language) {
        case "bg":
            analyzer = new BulgarianAnalyzer(stopset);
            break;
        case "de":
            analyzer = new GermanAnalyzer(stopset);
            break;
        case "es":
            analyzer = new SpanishAnalyzer(stopset);
            break;
        case "fa":
            analyzer = new PersianAnalyzer(stopset);
            break;
        case "fi":
            analyzer = new FinnishAnalyzer(stopset);
            break;
        case "fr":
            analyzer = new FrenchAnalyzer(stopset);
            break;
        case "hu":
            analyzer = new HungarianAnalyzer(stopset);
            break;
        case "it":
            analyzer = new ItalianAnalyzer(stopset);
            break;
        case "nl":
            analyzer = new DutchAnalyzer(stopset);
            break;
        case "pt":
            analyzer = new PortugueseAnalyzer(stopset);
            break;
        case "ru":
            analyzer = new RussianAnalyzer(stopset);
            break;
        case "sv":
            analyzer = new SwedishAnalyzer(stopset);
            break;
        default:
            throw new UnsupportedOperationException("Language not supported yet");
        }

    }

    return analyzer;

}

From source file:perLucene.Server.java

License:Open Source License

private static void initAnalyzers() {

    ha = new HashMap<String, Analyzer>();

    ha.put("ar", new ArabicAnalyzer(Version.LUCENE_41));
    ha.put("el", new GreekAnalyzer(Version.LUCENE_41));
    ha.put("bg", new BulgarianAnalyzer(Version.LUCENE_41));
    ha.put("br", new BrazilianAnalyzer(Version.LUCENE_41));
    ha.put("ca", new CatalanAnalyzer(Version.LUCENE_41));
    ha.put("cz", new CzechAnalyzer(Version.LUCENE_41));
    ha.put("da", new DanishAnalyzer(Version.LUCENE_41));
    ha.put("de", new GermanAnalyzer(Version.LUCENE_41));
    ha.put("en", new EnglishAnalyzer(Version.LUCENE_41));
    ha.put("es", new SpanishAnalyzer(Version.LUCENE_41));
    ha.put("eu", new BasqueAnalyzer(Version.LUCENE_41));
    ha.put("fa", new PersianAnalyzer(Version.LUCENE_41));
    ha.put("fi", new FinnishAnalyzer(Version.LUCENE_41));
    ha.put("fr", new FrenchAnalyzer(Version.LUCENE_41));
    ha.put("ga", new IrishAnalyzer(Version.LUCENE_41));
    ha.put("gl", new GalicianAnalyzer(Version.LUCENE_41));
    ha.put("hi", new HindiAnalyzer(Version.LUCENE_41));
    ha.put("hu", new HungarianAnalyzer(Version.LUCENE_41));
    ha.put("hy", new ArmenianAnalyzer(Version.LUCENE_41));
    ha.put("id", new IndonesianAnalyzer(Version.LUCENE_41));
    ha.put("it", new ItalianAnalyzer(Version.LUCENE_41));
    ha.put("lv", new LatvianAnalyzer(Version.LUCENE_41));
    ha.put("nl", new DutchAnalyzer(Version.LUCENE_41));
    ha.put("no", new NorwegianAnalyzer(Version.LUCENE_41));
    ha.put("pt", new PortugueseAnalyzer(Version.LUCENE_41));
    ha.put("ro", new RomanianAnalyzer(Version.LUCENE_41));
    ha.put("ru", new RussianAnalyzer(Version.LUCENE_41));
    ha.put("sv", new SwedishAnalyzer(Version.LUCENE_41));
    ha.put("th", new ThaiAnalyzer(Version.LUCENE_41));
    ha.put("tr", new TurkishAnalyzer(Version.LUCENE_41));
    ha.put("cn", new SmartChineseAnalyzer(Version.LUCENE_41));

}

From source file:practica1_2.IndexFiles.java

License:Apache License

/** Index all text files under a directory. */
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    String usage = "java org.apache.lucene.demo.IndexFiles"
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles";
    String indexPath = "index";
    String docsPath = null;//from www .  j  a  v  a 2s  . c  o  m
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];
            i++;
        } else if ("-docs".equals(args[i])) {
            docsPath = args[i + 1];
            i++;
        } else if ("-update".equals(args[i])) {
            create = false;
        }
    }

    if (docsPath == null) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(new File(indexPath));

        Analyzer analyzer = new SpanishAnalyzer(Version.LUCENE_44);

        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer. But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here. This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:practica1_2.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/*from w  w  w . j a va  2s.  c om*/
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new SpanishAnalyzer(Version.LUCENE_44);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }
    QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

        if (line == null || line.length() == -1) {
            break;
        }

        line = line.trim();
        if (line.length() == 0) {
            break;
        }

        Query query = parser.parse(line);
        System.out.println("Searching for: " + query.toString(field));

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:practica1_2.SearchFiles_P2.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/* w  ww . jav  a 2 s  . c  o  m*/
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new SpanishAnalyzer(Version.LUCENE_44);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }
    QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
    //    while (true) {
    //      if (queries == null && queryString == null) {                        // prompt the user
    //        System.out.println("Enter query: ");
    //      }

    //      String line = queryString != null ? queryString : in.readLine();
    //
    //      if (line == null || line.length() == -1) {
    //        break;
    //      }
    //
    //      line = line.trim();
    //      if (line.length() == 0) {
    //        break;
    //      }

    //Query query = parser.parse(line);
    //System.out.println("Searching for: " + query.toString(field));
    BooleanQuery query = new BooleanQuery();
    Double east = 180.0;
    Double west = -180.0;
    Double south = -90.0;
    Double north = 90.0;
    // Xmin <= east

    NumericRangeQuery<Double> westRangeQuery = NumericRangeQuery.newDoubleRange("west", null, east, true, true);
    query.add(westRangeQuery, BooleanClause.Occur.MUST);
    NumericRangeQuery<Double> eastRangeQuery = NumericRangeQuery.newDoubleRange("east", west, null, true, true);
    query.add(eastRangeQuery, BooleanClause.Occur.MUST);
    NumericRangeQuery<Double> southRangeQuery = NumericRangeQuery.newDoubleRange("south", null, north, true,
            true);
    query.add(southRangeQuery, BooleanClause.Occur.MUST);
    NumericRangeQuery<Double> northRangeQuery = NumericRangeQuery.newDoubleRange("north", south, null, true,
            true);
    query.add(northRangeQuery, BooleanClause.Occur.MUST);

    if (repeat > 0) { // repeat & time as benchmark
        Date start = new Date();
        for (int i = 0; i < repeat; i++) {
            searcher.search(query, 100);
        }
        Date end = new Date();
        System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
    }

    doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

    //      if (queryString != null) {
    //        break;
    //      }
    //    }
    reader.close();
}