2015-01-29 21 views
25

Tôi tạo tùy chỉnh Plugin Elasticsearch. Bây giờ tôi muốn viết một bài kiểm tra cho plugin này. sự mong đợi của tôi đã được - rằng tôi có thể chạy nhúng Elasticsearch dụ, thiết lập nó đúng cách và sau đó làm một số xét nghiệm (index một số tài liệu, sau đó truy vấn cho nó)Làm cách nào để viết bài kiểm tra cho plugin tùy chỉnh Elasticsearch?

Vấn đề là tôi không thể thiết lập Plugin của tôi đúng

đang Tuỳ chỉnh plugin được phân tích cú pháp JSON truy vấn và thiết lập một số đối tượng để sử dụng sau: mã

public class CustomQueryParserPlugin extends AbstractPlugin { 
    public static final String PLUGIN_NAME = "custom_query"; 
    private final Settings settings; 

    @Inject 
    public CustomQueryParserPlugin (Settings settings) { 
     this.settings = settings; 
    } 

    @Override 
    public String name() { 
     return PLUGIN_NAME; 
    } 

    @Override 
    public String description() { 
     return "custom plugin"; 
    } 

    public void onModule(IndicesQueriesModule module) { 
     module.addQuery(new CustomQueryParser(settings)); 
    } 
} 

Test:

public class CustomParserPluginTest extends ElasticsearchSingleNodeTest { 

    private static Node newNode() { 
     final Settings settings = ImmutableSettings.builder() 
       .put(ClusterName.SETTING, nodeName()) 
       .put("node.name", nodeName()) 
       .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) 
       .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) 
       .put(EsExecutors.PROCESSORS, 1) // limit the number of threads created 
       .put("http.enabled", false) 
       .put("plugin.types", CustomParserPlugin.class.getName()) 
       .put("path.plugins", pathToPlugin) 
       .put("index.store.type", "ram") 
       .put("config.ignore_system_properties", true) // make sure we get what we set :) 
       .put("gateway.type", "none").build(); 
     Node build = NodeBuilder.nodeBuilder().local(true).data(true).settings(
       settings).build(); 
     build.start(); 
     assertThat(DiscoveryNode.localNode(build.settings()), is(true)); 
     return build; 
    } 


    @Test 
    public void jsonParsing() throws URISyntaxException { 
     final Client client = newNode().client(); 
     final SearchResponse test = client.prepareSearch("test-index").setSource(addQuery()).execute().actionGet(); 
    } 

    private String addQuery() { 
     return "{"match_all":{"boost":1.2}}" 
    } 
.210

Tôi đã thử nhiều giá trị cho pathToPlugin - nhưng không có vẻ như hoạt động tốt, vì JSON truy vấn luôn cho tôi một ngoại lệ:

QueryParsingException[[test-index] No query registered for [custom_query]]; 

Tất cả các tài liệu tôi có thể tìm được về cài đặt plugin và thử nghiệm chúng trên một số cài đặt Elasticsearch địa phương.

Tôi đang làm gì sai ở đây? Có tài liệu hoặc ví dụ về các bài kiểm tra như thế không?

UPD. Dưới đây là một repo với mã được trích xuất của CustomQueryParserPlugin - https://github.com/MysterionRise/es-custom-parser

Có thể trong phần khởi tạo trong thử nghiệm tôi cần tạo trong chỉ mục bộ nhớ?

+1

Bạn có thể chia sẻ toàn bộ tệp với lớp 'CustomQueryParserPlugin' và cây thư mục cho plugin này không? – Rob

+0

@Rob, cảm ơn sự quan tâm của bạn, tôi đã tạo một repo với tất cả các mã được trích xuất cho ví dụ này. Tôi sẽ cập nhật câu hỏi ngay lập tức – Mysterion

Trả lời

5

Để viết kiểm tra cho bạn, bạn có thể sử dụng Elasticsearch Cluster Runner. Để tham khảo kiểm tra cách MinHash Plugin đã viết kiểm tra.

UPDATE:

Tôi đã thay đổi CustomParserPluginTest lớp để sử dụng Elasticsearch Cụm Runner:

import static org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs; 
import java.util.Map; 
import junit.framework.TestCase; 
import org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner; 
import org.elasticsearch.action.get.GetResponse; 
import org.elasticsearch.action.index.IndexResponse; 
import org.elasticsearch.client.Client; 
import org.elasticsearch.common.bytes.BytesArray; 
import org.elasticsearch.common.settings.ImmutableSettings; 
import org.elasticsearch.common.settings.ImmutableSettings.Builder; 
import org.elasticsearch.common.xcontent.XContentBuilder; 
import org.elasticsearch.common.xcontent.XContentFactory; 
import org.elasticsearch.index.get.GetField; 
import org.junit.Assert; 
import org.elasticsearch.action.search.SearchResponse; 
import static org.hamcrest.core.Is.is; 

public class CustomParserPluginTest extends TestCase { 

    private ElasticsearchClusterRunner runner; 

    @Override 
    protected void setUp() throws Exception { 
     // create runner instance 
     runner = new ElasticsearchClusterRunner(); 
     // create ES nodes 
     runner.onBuild(new ElasticsearchClusterRunner.Builder() { 
      @Override 
      public void build(final int number, final Builder settingsBuilder) { 
      } 
     }).build(newConfigs().ramIndexStore().numOfNode(1)); 

     // wait for yellow status 
     runner.ensureYellow(); 
    } 

    @Override 
    protected void tearDown() throws Exception { 
     // close runner 
     runner.close(); 
     // delete all files 
     runner.clean(); 
    } 

    public void test_jsonParsing() throws Exception { 
     final String index = "test_index"; 

     runner.createIndex(index, ImmutableSettings.builder().build()); 
     runner.ensureYellow(index); 

     final SearchResponse test = runner.client().prepareSearch(index).setSource(addQuery()).execute().actionGet(); 
    } 

    private String addQuery() { 
     return "{\"match_all\":{\"boost\":1.2}}"; 
    } 
} 

Tôi đã tạo es-plugin.properties (pluginrootdirectory \ src \ resources \ chính) tập tin với nội dung sau đây mà sẽ buộc trường hợp elasticsearch tải plugin:

plugin=CustomQueryParserPlugin 

Khi bạn muốn bệnh chạy thử nghiệm này, bạn sẽ thấy trong đầu ra rằng các insance mới được tạo ra của elasticsearch tải các plugin.

[2015/04/29 19: 22: 10.783] [INFO] [org.elasticsearch.node] [Node 1] phiên bản [1,5 0,0], pid [34.360], xây dựng [5448160/2015 -03-23T14: 30: 58Z] [2015-04-29 19: 22: 10,784] [INFO] [org.elasticsearch.node] [Nút 1] initializin g ... [2015-04-29 19: 22: 10,795] [INFO ] [org.elasticsearch.plugins] [Nút 1] được tải [custom_query], các trang web [] [2015-04-29 19: 22: 13,342] [INFO] [org.elasticsearch. nút] [Nút 1] được khởi tạo

[2015-04-29 19: 22: 13,342] [INFO] [org.elasticsear ch.nút] [Nút 1] bắt đầu ...

Hy vọng điều này sẽ hữu ích.

+0

Lưu ý rằng kể từ phiên bản 2.1 Elasticsearch không tải các plugin từ classpath nữa. Xem thảo luận tại đây: https://discuss.elastic.co/t/add-plugins-from-classpath-in-embedded-elasticsearch-client-node/36269/6 Để plugin vẫn tải plugin của bạn cần cung cấp cài đặt 'plugin.types' với FQN của plugin (được phân cách bằng dấu phẩy trong trường hợp có nhiều) – Dzmitry

Các vấn đề liên quan