简体   繁体   English

Elasticsearch构建SQL Server索引并创建搜索查询失败

[英]Elasticsearch build sql server index and create search query failes

After reading and trying several of articles and getting no result.. 阅读并尝试了几篇文章之后,没有任何结果。

I want to create and elasticsearch query that returns data base results 我想创建并返回数据库结果的Elasticsearch查询

Example : 范例

[Step 1]: my db is [my_db] and my table name is [my_table] to build new index on localhost:9200 [步骤1]:我的数据库是[my_db],表名是[my_table],以便在localhost:9200上建立新索引

POST /my_index/my_type/_meta 
{
    "type":"jdbc",
    "jdbc": 
    {
        "driver":"com.microsoft.sqlserver.jdbc.SQLServerDriver",
        "url":"jdbc:sqlserver://[my_db_ip];databaseName=[my_db]",
        "user":"sa","password":"xxxxxx",
        "sql":"SELECT * FROM [my_table]",
        "poll":"5s",
        "index": "my_index",
        "type": "my_type"     
    }
}

The index creation result: 索引创建结果:

{
   "_index": "my_index",
   "_type": "my_type",
   "_id": "_meta",
   "_version": 1,
   "created": true
}

[Step 2]: The search query [步骤2]:搜寻查询

POST /my_index/_search
{
    "query_string" : {
        "query" : "FreeText"
    }
}

The search result 搜索结果

{
   "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures....
}

what is wrong with my search query?? 我的搜索查询出了什么问题?

how can i create a query that returns results from [my_table] rows? 我如何创建一个从[my_table]行返回结果的查询?

Try the match_all query (see here for the official documentation). 尝试match_all查询(请参阅此处获取官方文档)。 This will bring you all the results of my_type. 这将为您带来my_type的所有结果。

Example: 例:

POST /my_index/my_type/_search 
{
   "query": { "match_all": {} } 
}

If you need to search for some specific term then you must pay attention to the mappings of your type and the query type that you'll use. 如果需要搜索某些特定术语,则必须注意类型和将要使用的查询类型映射

Update 更新资料

Mappings: 映射:

From the schema of your table I understand that the below mappings for my_type would suit you well. 从表的模式中,我了解到my_type的以下映射非常适合您。

{
    "my_table" : {
        "properties" : {
            "orderid" : {"type" : "integer", "index" : "not_analyzed"},
            "ordername " : {"type" : "string" }
        }
    }
}

Keep in mind that if the data are already indexed you cannot change the mappings. 请记住,如果已经为数据建立索引,则无法更改映射。 You must reindex your data after defining the proper mapping. 定义正确的映射后,必须重新索引数据。

Generally I'd propose you to follow the below methodology: 通常,我建议您遵循以下方法:

  1. Create your index with the index settings that you need 使用所需的索引设置创建索引
  2. Define the mappings of your type 定义您的类型的映射
  3. Index your data 索引您的数据

Do not mingle all of these steps in one and avoid leaving things in luck (like default mappings). 不要将所有这些步骤混为一谈,避免碰运气(例如默认映射)。

You can use the match query in order to search the data of a field on the document. 您可以使用匹配查询来搜索文档中字段的数据。

Example

POST /my_index/my_type/_search 
{
    "query": { 
        "match": {
           "FIELD": "TEXT"
        }
    } 
}

You can use the multi-match query in order to search the data of multiple fields of the document. 您可以使用多重匹配查询来搜索文档中多个字段的数据。

Example

POST /my_index/my_type/_search 
{
    "query": { 
        "multi_match": {
           "query":    "TEXT", 
           "fields": [ "field1", "field2" ]
        }
    } 
}

For more querying options check the official documentation on Query DSL . 有关更多查询选项,请参阅Query DSL上的官方文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM