Search in sources :

Example 76 with Index

use of org.elasticsearch.index.Index in project elasticsearch by elastic.

the class SimpleIcuNormalizerCharFilterTests method testNameAndModeSetting.

public void testNameAndModeSetting() throws Exception {
    Settings settings = Settings.builder().put("index.analysis.char_filter.myNormalizerChar.type", "icu_normalizer").put("index.analysis.char_filter.myNormalizerChar.name", "nfkc").put("index.analysis.char_filter.myNormalizerChar.mode", "decompose").build();
    TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin());
    CharFilterFactory charFilterFactory = analysis.charFilter.get("myNormalizerChar");
    String input = "ʰ㌰゙5℃№㈱㌘,バッファーの正規化のテスト.㋐㋑㋒㋓㋔カキクケコザジズゼゾg̈각/각நிเกषिchkʷक्षि";
    Normalizer2 normalizer = Normalizer2.getInstance(null, "nfkc", Normalizer2.Mode.DECOMPOSE);
    String expectedOutput = normalizer.normalize(input);
    CharFilter inputReader = (CharFilter) charFilterFactory.create(new StringReader(input));
    char[] tempBuff = new char[10];
    StringBuilder output = new StringBuilder();
    while (true) {
        int length = inputReader.read(tempBuff);
        if (length == -1)
            break;
        output.append(tempBuff, 0, length);
        assertEquals(output.toString(), normalizer.normalize(input.substring(0, inputReader.correctOffset(output.length()))));
    }
    assertEquals(expectedOutput, output.toString());
}
Also used : Normalizer2(com.ibm.icu.text.Normalizer2) CharFilter(org.apache.lucene.analysis.CharFilter) StringReader(java.io.StringReader) Index(org.elasticsearch.index.Index) AnalysisICUPlugin(org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin) Settings(org.elasticsearch.common.settings.Settings)

Example 77 with Index

use of org.elasticsearch.index.Index in project elasticsearch by elastic.

the class SimpleUkrainianAnalyzerTests method testAnalyzer.

private static void testAnalyzer(String source, String... expected_terms) throws IOException {
    TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisUkrainianPlugin());
    Analyzer analyzer = analysis.indexAnalyzers.get("ukrainian").analyzer();
    TokenStream ts = analyzer.tokenStream("test", source);
    CharTermAttribute term1 = ts.addAttribute(CharTermAttribute.class);
    ts.reset();
    for (String expected : expected_terms) {
        assertThat(ts.incrementToken(), equalTo(true));
        assertThat(term1.toString(), equalTo(expected));
    }
    assertThat(ts.incrementToken(), equalTo(false));
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) Index(org.elasticsearch.index.Index) Analyzer(org.apache.lucene.analysis.Analyzer) AnalysisUkrainianPlugin(org.elasticsearch.plugin.analysis.ukrainian.AnalysisUkrainianPlugin)

Example 78 with Index

use of org.elasticsearch.index.Index in project elasticsearch by elastic.

the class PolishAnalysisTests method testDefaultsPolishAnalysis.

public void testDefaultsPolishAnalysis() throws IOException {
    final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisStempelPlugin());
    TokenFilterFactory tokenizerFactory = analysis.tokenFilter.get("polish_stem");
    MatcherAssert.assertThat(tokenizerFactory, instanceOf(PolishStemTokenFilterFactory.class));
    Analyzer analyzer = analysis.indexAnalyzers.get("polish").analyzer();
    MatcherAssert.assertThat(analyzer, instanceOf(PolishAnalyzer.class));
}
Also used : PolishAnalyzer(org.apache.lucene.analysis.pl.PolishAnalyzer) Index(org.elasticsearch.index.Index) PolishStemTokenFilterFactory(org.elasticsearch.index.analysis.pl.PolishStemTokenFilterFactory) AnalysisStempelPlugin(org.elasticsearch.plugin.analysis.stempel.AnalysisStempelPlugin) PolishAnalyzer(org.apache.lucene.analysis.pl.PolishAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) PolishStemTokenFilterFactory(org.elasticsearch.index.analysis.pl.PolishStemTokenFilterFactory)

Example 79 with Index

use of org.elasticsearch.index.Index in project elasticsearch by elastic.

the class NodeEnvironmentTests method testShardLock.

public void testShardLock() throws Exception {
    final NodeEnvironment env = newNodeEnvironment();
    Index index = new Index("foo", "fooUUID");
    ShardLock fooLock = env.shardLock(new ShardId(index, 0));
    assertEquals(new ShardId(index, 0), fooLock.getShardId());
    try {
        env.shardLock(new ShardId(index, 0));
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    for (Path path : env.indexPaths(index)) {
        Files.createDirectories(path.resolve("0"));
        Files.createDirectories(path.resolve("1"));
    }
    try {
        env.lockAllForIndex(index, idxSettings, randomIntBetween(0, 10));
        fail("shard 0 is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    fooLock.close();
    // can lock again?
    env.shardLock(new ShardId(index, 0)).close();
    List<ShardLock> locks = env.lockAllForIndex(index, idxSettings, randomIntBetween(0, 10));
    try {
        env.shardLock(new ShardId(index, 0));
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    IOUtils.close(locks);
    assertTrue("LockedShards: " + env.lockedShards(), env.lockedShards().isEmpty());
    env.close();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Path(java.nio.file.Path) Index(org.elasticsearch.index.Index)

Example 80 with Index

use of org.elasticsearch.index.Index in project elasticsearch by elastic.

the class NodeEnvironmentTests method testDeleteSafe.

public void testDeleteSafe() throws Exception {
    final NodeEnvironment env = newNodeEnvironment();
    final Index index = new Index("foo", "fooUUID");
    ShardLock fooLock = env.shardLock(new ShardId(index, 0));
    assertEquals(new ShardId(index, 0), fooLock.getShardId());
    for (Path path : env.indexPaths(index)) {
        Files.createDirectories(path.resolve("0"));
        Files.createDirectories(path.resolve("1"));
    }
    try {
        env.deleteShardDirectorySafe(new ShardId(index, 0), idxSettings);
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    for (Path path : env.indexPaths(index)) {
        assertTrue(Files.exists(path.resolve("0")));
        assertTrue(Files.exists(path.resolve("1")));
    }
    env.deleteShardDirectorySafe(new ShardId(index, 1), idxSettings);
    for (Path path : env.indexPaths(index)) {
        assertTrue(Files.exists(path.resolve("0")));
        assertFalse(Files.exists(path.resolve("1")));
    }
    try {
        env.deleteIndexDirectorySafe(index, randomIntBetween(0, 10), idxSettings);
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    fooLock.close();
    for (Path path : env.indexPaths(index)) {
        assertTrue(Files.exists(path));
    }
    final AtomicReference<Throwable> threadException = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch blockLatch = new CountDownLatch(1);
    final CountDownLatch start = new CountDownLatch(1);
    if (randomBoolean()) {
        Thread t = new Thread(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                logger.error("unexpected error", e);
                threadException.set(e);
                latch.countDown();
                blockLatch.countDown();
            }

            @Override
            protected void doRun() throws Exception {
                start.await();
                try (ShardLock autoCloses = env.shardLock(new ShardId(index, 0))) {
                    blockLatch.countDown();
                    Thread.sleep(randomIntBetween(1, 10));
                }
                latch.countDown();
            }
        });
        t.start();
    } else {
        latch.countDown();
        blockLatch.countDown();
    }
    start.countDown();
    blockLatch.await();
    env.deleteIndexDirectorySafe(index, 5000, idxSettings);
    assertNull(threadException.get());
    for (Path path : env.indexPaths(index)) {
        assertFalse(Files.exists(path));
    }
    latch.await();
    assertTrue("LockedShards: " + env.lockedShards(), env.lockedShards().isEmpty());
    env.close();
}
Also used : Path(java.nio.file.Path) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Index(org.elasticsearch.index.Index) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ShardId(org.elasticsearch.index.shard.ShardId)

Aggregations

Index (org.elasticsearch.index.Index)366 ShardId (org.elasticsearch.index.shard.ShardId)108 Settings (org.elasticsearch.common.settings.Settings)95 ClusterState (org.elasticsearch.cluster.ClusterState)88 ArrayList (java.util.ArrayList)79 IOException (java.io.IOException)74 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)65 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)65 HashMap (java.util.HashMap)61 Map (java.util.Map)61 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)59 List (java.util.List)56 HashSet (java.util.HashSet)50 Path (java.nio.file.Path)45 IndexSettings (org.elasticsearch.index.IndexSettings)44 IndexService (org.elasticsearch.index.IndexService)43 Set (java.util.Set)40 Metadata (org.elasticsearch.cluster.metadata.Metadata)39 ClusterService (org.elasticsearch.cluster.service.ClusterService)39 ActionListener (org.elasticsearch.action.ActionListener)35