0

我正在尝试将 Sql Server 中的数据加载到 ElasticSearch 中。我将 Logstash 与 jdbc 插件和弹性搜索插件一起使用。我在 ElasticSearch 中加载我的数据,但不知道如何设置我的索引。我正在使用模板索引来尝试这个。以下是我正在使用的,但每当我搜索时,我都没有得到任何结果。

logstash.config

# contents of logstash\bin\logstash.config

input {
  jdbc {
    jdbc_driver_library => ".\Microsoft JDBC Driver 6.2 for SQL Server\sqljdbc_6.2\enu\mssql-jdbc-6.2.1.jre8.jar"
    jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    jdbc_connection_string => "jdbc:sqlserver://mydbserver;databaseName=mydb;"
    jdbc_user => "******"
    jdbc_password => "******"
    schedule => "* * * * *"
    parameters => { "classification" => "EMPLOYEE" }
    statement => "SELECT Cost_Center, CC_Acct_1, CC_Acct_2, CC_Acct_3 from dbo.Cost_Center where CC_Classification = :classification"
  }
}
output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "allocation-testweb"
    template => "index_template.json"
  }
  #stdout { codec => rubydebug }
}

index_template.json

{
"template": "allocation-*",
"order":1,
"settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1,
    "analysis": {
        "analyzer": {
            "substring_analyzer": {
                "tokenizer": "ngram_tokenizer",
                "filter": ["lowercase"]
            }
        },
        "tokenizer": {
            "ngram_tokenizer": {
                "type": "edge_ngram",
                "min_gram": 2,
                "max_gram": 10,
                "token_chars": ["letter","digit"]
            }
        }
    }
},
"mappings":{
    "costcenter": {
        "properties": {
            "cc_acct_1": {
                "type": "string",
                "analyzer": "substring_analyzer"
            },
            "cc_acct_2": {
                "type": "string",
                "analyzer": "substring_analyzer"
            }
        }
    }
}

在进行一些初步研究时,我在代码中创建了一个类似的索引。我的 index_template 不正确还是我应该这样做?

更新:我的 2 个文件之间的索引名称不匹配。我现在可以使用 Postman 和 curl 进行搜索。但是,当我尝试使用 NEST 客户端获取数据时,我永远无法取回数据。下面是查询的代码片段。

  var searchResult = client.Search<CostCenter>(s => s  
    .Size(1000)
    .Index("allocation_testweb")
    .MatchAll());

这以前适用于从文件加载的相同数据。CostCenter 只是一个对象,其成员名为 Cost_Center、CC_Acct_1、CC_Acct_2 和 CC_Acct_3。我再次确定我将问题复杂化并且遗漏了一些明显的东西。

更新二:

我已经做出了下面@RussCam 建议的更改,但仍然没有得到任何结果。下面是我更新的代码。

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node); 
     //.InferMappingFor<CostCenter>(m => m.IndexName("allocation_testweb"));
var client = new ElasticClient(settings);

var searchResult = client.Search<CostCenter>(s => s
    .Type("costCenter")
    .Size(1000)
    .Index("allocation_testweb")
    .MatchAll());

我注释掉了 InferMappingFor<> 因为它没有提供结果。

@RussCam 请求的映射图像。我还包括了我的成本中心类(我尝试命名成本中心的所有变体)。

public class costcenter
{
  public string cost_center { get; set; }
  public string cc_acct_1 { get; set; }
  public string cc_acct_2 { get; set; }
  public string cc_acct_3 { get; set; }
}

在此处输入图像描述

4

0 回答 0