我如何在 ElasticSearch 中使用 sql 之类的子句?

How can i use sql like and clauses in ElasticSearch?

我阅读了这篇文档以了解 elasticsearch 中 sql 的相等性。(https://taohiko.wordpress.com/2014/07/18/query-dsl-elasticsearch-vs-sql/)我开发了一个类似 elasticsearch 的应用程序,如果我在下面调用 post,它会根据我的数据创建索引使用 postman :

查询

{
  "query": {
    "multi_match" : {
      "query":      "TÜRKİYE iş 3124904300",
      "type":       "cross_fields",
      "fields":     [ "title", "tcknVkn","phone","townName","cityName","poiDesc","district","street","avenue","buildingName","addressName" ],
      "operator":   "and" 
    }
  }
}

它运行完美。但我想那样做;


{
  "query": {
    "multi_match" : {
      "query":      "TÜRKİYE iş 312*",
      "type":       "cross_fields",
      "fields":     [ "title", "tcknVkn","phone","townName","cityName","poiDesc","district","street","avenue","buildingName","addressName" ],
      "operator":   "and" 
    }
  }
}

表示:


select * from mytable where title like 'TÜRKİYE%' and addressName like 'iş%' and 
tcknVkn like '312%'

但是。如果我在上面写查询 elasticsearch 无法理解数值。所以它 returns 我的数据是空的?我怎么解决这个问题。我如何将上面的 sql 查询转换为 elasticsearch 查询?

(注意:"title"、"tcknVkn"、"phone"、"townName"、"cityName"、"poiDesc","district","street","avenue","buildingName","addressName" -> 所有字段都索引为字符串)

multi_match 查询本质上是创建多个匹配查询。您的查询创建如下查询:

{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":   "TÜRKİYE iş 312*" }},
        { "match": { "tcknVkn": "TÜRKİYE iş 312*" }},
        { "match": { "phone":   "TÜRKİYE iş 312*" }},
        ...
      ]
    }
  }
}

它与上面的示例不完全相同,但相似。但是您想将查询关键字分开。我创建以下示例来说明您的情况:

POST test2/test/_mapping
{
  "properties": {
    "tcNo": {
      "type": "long",
      "fields": {
        "text": {
          "type": "text"
        },
        "numeric": {
          "type": "integer"
        }
      }
    }
  }
}

POST test2/test
{
  "id": 0,
  "tcNo": 23432344,
  "name": "hay0"
}

POST test2/test
{
  "id": 1,
  "tcNo": 23442344,
  "name": "haydar1"
}

POST test2/test
{
  "id": 2,
  "tcNo": 23432344,
  "name": "haydar2"
}

POST test2/test
{
  "id": 3,
  "tcNo": 23542344,
  "name": "haydar3"
}


GET test2/_search
{
  "query": {
    "multi_match" : {
      "query":      "haydar* 234*",
      "type":       "phrase_prefix",
      "fields":     ["tcNo.text", "name"],
      "operator":   "AND" 
    }
  }
}

POST test2/test/_search
{
  "query": {
    "query_string": {
      "fields": ["tcNo.text", "name"],
      "query": "haydar* AND 234*"
    }
  }
}

# similar with last one
POST test2/test/_search
{
  "query": {
    "query_string": {
      "query": "(tcNo.text:234* OR name.text:234*) AND (tcNo.text:haydar* OR name:haydar*)"
    }
  }
}

是的,我知道您所有的字段字符串。但我更喜欢对整数数据使用 long 或整数类型。在查询结束时,multi_match 一个是 return 没有结果但是 query_string return 两个是正确的结果。因此,您可以使用 query_string 查询进行搜索。