Search in sources :

Example 26 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project querydsl by querydsl.

the class LuceneSerializerTest method setUp.

@Before
public void setUp() throws Exception {
    serializer = new LuceneSerializer(true, true);
    entityPath = new PathBuilder<Object>(Object.class, "obj");
    title = entityPath.getString("title");
    author = entityPath.getString("author");
    text = entityPath.getString("text");
    publisher = entityPath.getString("publisher");
    year = entityPath.getNumber("year", Integer.class);
    rating = entityPath.getString("rating");
    gross = entityPath.getNumber("gross", Double.class);
    titles = entityPath.getCollection("title", String.class, StringPath.class);
    longField = entityPath.getNumber("longField", Long.class);
    shortField = entityPath.getNumber("shortField", Short.class);
    byteField = entityPath.getNumber("byteField", Byte.class);
    floatField = entityPath.getNumber("floatField", Float.class);
    idx = new RAMDirectory();
    config = new IndexWriterConfig(Version.LUCENE_42, new StandardAnalyzer(Version.LUCENE_42)).setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);
    writer.addDocument(createDocument());
    writer.close();
    IndexReader reader = IndexReader.open(idx);
    searcher = new IndexSearcher(reader);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Before(org.junit.Before)

Example 27 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project jforum2 by rafaelsteil.

the class LuceneSearch method performSearch.

private SearchResult performSearch(SearchArgs args, LuceneResultCollector resultCollector, Filter filter) {
    SearchResult result;
    try {
        StringBuffer criteria = new StringBuffer(256);
        this.filterByForum(args, criteria);
        this.filterByKeywords(args, criteria);
        this.filterByDateRange(args, criteria);
        Query query = new QueryParser("", new StandardAnalyzer()).parse(criteria.toString());
        if (logger.isDebugEnabled()) {
            logger.debug("Generated query: " + query);
        }
        Hits hits = filter == null ? this.search.search(query, this.getSorter(args)) : this.search.search(query, filter, this.getSorter(args));
        if (hits != null && hits.length() > 0) {
            result = new SearchResult(resultCollector.collect(args, hits, query), hits.length());
        } else {
            result = new SearchResult(new ArrayList(), 0);
        }
    } catch (Exception e) {
        throw new SearchException(e);
    }
    return result;
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Hits(org.apache.lucene.search.Hits) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ArrayList(java.util.ArrayList) SearchException(net.jforum.exceptions.SearchException) IOException(java.io.IOException) SearchException(net.jforum.exceptions.SearchException)

Example 28 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project elasticsearch-suggest-plugin by spinscale.

the class AbstractCacheLoaderSuggester method load.

@Override
public T load(ShardSuggestService.FieldType fieldType) throws Exception {
    MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(fieldType.field(), fieldType.types());
    Analyzer queryAnalyzer = null;
    Analyzer indexAnalyzer = null;
    if (fieldMappers != null) {
        FieldMapper fieldMapper = mapperService.smartName(fieldType.field(), fieldType.types()).mapper();
        queryAnalyzer = fieldMapper.searchAnalyzer();
        if (Strings.hasLength(fieldType.indexAnalyzer())) {
            NamedAnalyzer namedAnalyzer = analysisService.analyzer(fieldType.queryAnalyzer());
            if (namedAnalyzer == null) {
                throw new ElasticsearchException("Query analyzer[" + fieldType.queryAnalyzer() + "] does not exist.");
            }
            queryAnalyzer = namedAnalyzer.analyzer();
        }
        indexAnalyzer = fieldMapper.searchAnalyzer();
        if (Strings.hasLength(fieldType.indexAnalyzer())) {
            NamedAnalyzer namedAnalyzer = analysisService.analyzer(fieldType.indexAnalyzer());
            if (namedAnalyzer == null) {
                throw new ElasticsearchException("Index analyzer[" + fieldType.indexAnalyzer() + "] does not exist.");
            }
            indexAnalyzer = namedAnalyzer.analyzer();
        }
    }
    if (queryAnalyzer == null) {
        queryAnalyzer = new StandardAnalyzer(org.elasticsearch.Version.CURRENT.luceneVersion);
    }
    if (indexAnalyzer == null) {
        indexAnalyzer = new StandardAnalyzer(org.elasticsearch.Version.CURRENT.luceneVersion);
    }
    return getSuggester(indexAnalyzer, queryAnalyzer, fieldType);
}
Also used : NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ElasticsearchException(org.elasticsearch.ElasticsearchException) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 29 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project languagetool by languagetool-org.

the class SimilarWordFinder method createIndex.

private void createIndex(List<String> words, File indexDir) throws IOException {
    FSDirectory dir = FSDirectory.open(indexDir.toPath());
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
    System.out.println("Creating index...");
    int docs = 0;
    try (IndexWriter writer = new IndexWriter(dir, indexWriterConfig)) {
        for (String word : words) {
            Document doc = new Document();
            doc.add(new TextField("word", word, Field.Store.YES));
            writer.addDocument(doc);
            docs++;
        }
    }
    System.out.println("Index created: " + docs + " docs");
}
Also used : StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) TextField(org.apache.lucene.document.TextField) FSDirectory(org.apache.lucene.store.FSDirectory) Document(org.apache.lucene.document.Document)

Example 30 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project titan by thinkaurelius.

the class LuceneExample method example1.

@Test
public void example1() throws Exception {
    Directory dir = FSDirectory.open(path);
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(dir, iwc);
    indexDocs(writer, "doc1", ImmutableMap.of("name", "The laborious work of John Doe as we know it", "city", "Blumenkamp", "location", Geoshape.point(51.687882, 6.612053), "time", 1000342034));
    indexDocs(writer, "doc2", ImmutableMap.of("name", "Life as we know it or not", "city", "Essen", "location", Geoshape.point(51.787882, 6.712053), "time", 1000342034 - 500));
    indexDocs(writer, "doc3", ImmutableMap.of("name", "Berlin - poor but sexy and a display of the extraordinary", "city", "Berlin", "location", Geoshape.circle(52.509535, 13.425293, 50), "time", 1000342034 + 2000));
    writer.close();
    //Search
    IndexReader reader = DirectoryReader.open(FSDirectory.open(path));
    IndexSearcher searcher = new IndexSearcher(reader);
    analyzer = new StandardAnalyzer();
    //Auesee
    BooleanFilter filter = new BooleanFilter();
    //filter.add(new TermsFilter(new Term("name_txt","know")), BooleanClause.Occur.MUST);
    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, Geoshape.circle(51.666167, 6.58905, 450).convert2Spatial4j());
    //filter.add(getSpatialStrategy("location").makeFilter(args), BooleanClause.Occur.MUST);
    filter.add(NumericRangeFilter.newLongRange("time", (long) 1000342034, (long) 1000342034, true, true), BooleanClause.Occur.MUST);
    //        filter.add(NumericRangeFilter.newLongRange("time",(long)1000342034-100,Long.MAX_VALUE,true,true), BooleanClause.Occur.MUST);
    //        filter.add(NumericRangeFilter.newLongRange("time",Long.MIN_VALUE,(long)1000342034+300,true,true), BooleanClause.Occur.MUST);
    filter.add(new PrefixFilter(new Term("city_str", "B")), BooleanClause.Occur.MUST);
    TopDocs docs = searcher.search(new MatchAllDocsQuery(), filter, MAX_RESULT);
    if (docs.totalHits >= MAX_RESULT)
        throw new RuntimeException("Max results exceeded: " + MAX_RESULT);
    Set<String> result = getResults(searcher, docs);
    System.out.println(result);
}
Also used : BooleanFilter(org.apache.lucene.queries.BooleanFilter) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) Test(org.junit.Test)

Aggregations

StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)112 Analyzer (org.apache.lucene.analysis.Analyzer)37 IndexWriter (org.apache.lucene.index.IndexWriter)36 Document (org.apache.lucene.document.Document)29 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)29 IndexSearcher (org.apache.lucene.search.IndexSearcher)24 Term (org.apache.lucene.index.Term)22 RAMDirectory (org.apache.lucene.store.RAMDirectory)21 Test (org.junit.Test)21 Query (org.apache.lucene.search.Query)20 BooleanQuery (org.apache.lucene.search.BooleanQuery)19 TermQuery (org.apache.lucene.search.TermQuery)19 IOException (java.io.IOException)16 Before (org.junit.Before)15 IndexReader (org.apache.lucene.index.IndexReader)14 HashMap (java.util.HashMap)13 Field (org.apache.lucene.document.Field)13 ArrayList (java.util.ArrayList)12 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)12 Directory (org.apache.lucene.store.Directory)12