【问题标题】:Convert SQL to ElasticSearch Query将 SQL 转换为 ElasticSearch 查询
【发布时间】:2016-12-19 12:33:48
【问题描述】:

我有这个 sql

Select * from a where a.col1 like %search_str% OR a.col2 like %search_str%  OR a.col3 like %search_str%;

什么是弹性搜索查询?

用过这个:

var query = {
      "query" => {
          "multi_match" => {
                  "query" => "#{search_str}",
                  "fields" =>["col1","col2","col3"]
          }
      }
  }

没有得到非常准确的结果。

【问题讨论】:

标签: elasticsearch


【解决方案1】:

试试下面的代码

{
"bool": {
    "should": [
        {
          "multi_match" : {
            "query":      "#{search_str}",
            "type":       "cross_fields",
            "fields":     [ "col1", "col2", "col3" ],
            "fuzziness": 2,
            "minimum_should_match": "50%" 
          }
        }
    ]
  }
}

【讨论】:

  • 如果他的索引值为“hello world”并且搜索词是“llo”,那么假设如何工作
  • 如果您还想捕获“llo”,则需要在上述 bool 查询中包含一个额外的“fuzzy_query”(编辑距离 2)。
【解决方案2】:

如果您将研究限制为相等或相似的操作,则无需分析文本。

将映射更改为关键字以避免分析,然后使用通配符或正则表达式在整个文本块中进行搜索。

详情请见here

PUT _template/test
{
  "template":   "test-*",
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "_default_": {
      "_all": { 
        "enabled": false
      },
      "dynamic_templates": [
        {
          "strings_as_keywords": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword",
              "normalizer": "my_normalizer"
            }
          }
        }
      ]
    }
  }
}

这是一个为所有以 test- 开头的新索引创建模板的示例,即:

  • 将所有字符串映射为没有字符限制的简单关键字
  • 为不区分大小写、不区分重音的研究对输入中的数据进行规范化
  • 删除 _all 字段

您可能希望将查询转换为过滤器以完全匹配您的项目并且不使用评分系统

GET /test-1/movies/_search
{ 
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "wildcard": {
                "title": "*tar war*"
              }
            },
            {
              "wildcard": {
                "title": "*ord of the ring*"
              }
            }
          ]
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2018-04-07
    • 2019-11-22
    • 2018-10-23
    • 1970-01-01
    • 2018-06-28
    • 2015-08-03
    • 2018-09-17
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多