ElasticSearch中查询语句用法(match、match_phrase、multi_match、query_string)

1、match

1.1 不同字段权重

        如果需要为不同字段设置不同权重,可以考虑使用 bool 查询的 should 子句来组合多个 match 查询,并为每个 match 查询设置不同的权重。

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "product_name": {
              "query": "apple",
              "boost": 3
            }
          }
        },
        {
          "match": {
            "description": {
              "query": "apple",
              "boost": 1
            }
          }
        }
      ]
    }
  }
}

        上面的查询将在 product_name 字段和 description 字段中搜索包含 "apple" 的文档,并为 product_name 字段设置权重为 3,而为 description 字段设置权重为 1。这样,在计算匹配得分时,product_name 字段的匹配将比 description 字段的匹配更加重要,因为它的权重更高。

        这种方式可以灵活地控制不同字段的权重,以满足不同的搜索需求。

2、match_pharse

        match_phrase 查询是 Elasticsearch 中一种用于精确匹配短语的查询方式,可以确保查询字符串中的关键词按照给定的顺序在文档中连续出现。以下是 match_phrase 查询的用法:

2.1 简单用法

        match_phrase 查询可以直接指定一个字段和一个短语进行匹配。

GET grade2/_search
{
  "query": {
    "match_phrase": {
      "character": "谦虚 态度"
    }
  },
  "track_total_hits": true
}

上面的查询将在 character字段中搜索包含短语 "谦虚 态" 的文档。

2.2 位置信息

        match_phrase 查询会记录匹配短语在文档中的位置信息,可以通过 slop 参数指定允许的位置偏移量。

GET grade2/_search
{
  "query": {
    "match_phrase": {
      "character": {
        "query": "谦虚 赞扬",
        "slop": 2
      }
    }
  },
  "track_total_hits": true
}

3、multi_match

        multi_match 查询是 Elasticsearch 中一种用于在多个字段中搜索相同查询字符串的查询方式。它可以在多个字段之间执行相同的查询,并且可以指定不同字段之间的权重(boost),从而影响匹配的相对重要性。

3.1 简单用法

        multi_match 查询可以直接指定一个查询字符串,然后在多个字段中进行搜索。

GET grade2/_search
{
  "query": {
    "multi_match": {
      "query": "张一",
      "fields": ["name", "character"]
    }
  },
  "track_total_hits": true
}

上面的查询将在 name和 character字段中搜索包含 "张一" 的文档。

3.2 类型匹配

multi_match 查询可以通过 type 参数指定匹配的类型,如 "best_fields"、 "most_fields"、 "cross_fields"、 "phrase"、 "phrase_prefix" 等。不同的类型在匹配方式和结果计算上有所不同。

GET grade2/_search
{
  "query": {
    "multi_match": {
      "query": "张一",
      "fields": ["name", "character"],
      "type": "best_fields"
    }
  }
}

上面的查询将使用 "best_fields" 类型在 name 和 character字段中搜索包含短语 "张一" 的文档。

4、query_string

        在 Elasticsearch 中,query_string 是一种查询方式,用于在文本字段上执行灵活且强大的搜索操作。query_string 查询支持使用 Lucene 查询语法进行高级搜索,可以通过在查询字符串中指定不同的搜索条件、操作符和逻辑关系来构建复杂的搜索查询。

4.1 简单的关键词匹配

GET grade2/_search
{
  "query": {
    "query_string": {
      "default_field": "character",
      "query": "乐观"
    }
  },
  "track_total_hits": true
}

上面的查询将在 character字段中搜索包含关键词 "乐观" 的文档。

4.2 使用逻辑关系和操作符进行组合查询:

GET grade2/_search
{
  "query": {
    "query_string": {
      "default_field": "character",
      "query": "乐观 OR (赞扬 AND 优越)"
    }
  },
  "track_total_hits": true
}

上面的查询将在 character字段中搜索包含关键词 "乐观" 或者 “赞扬 和 优越”的文档。

GET grade2/_search
{
  "query": {
    "query_string": {
      "default_field": "character",
      "query": "乐观 OR (name:刘一 AND age:25 AND 优越)"
    }
  },
  "track_total_hits": true
}

        上面的查询将在 character字段中搜索包含关键词 "乐观" 或者 name字段为 "刘一" 且 age字段为"25" 且 character字段为 “优越” 的文档。

4.3 模糊搜索和通配符搜索

GET account_info/_search
{
  "query": {
    "query_string": {
      "default_field": "email",
      "query": "qq?com~"
    }
  }
}
GET account_info/_search
{
  "query": {
    "query_string": {
      "default_field": "email",
      "query": "qqcom~"
    }
  }
}

        上面的查询将在 email字段中搜索类似于 "qq?com" 的词,其中 "?" 表示单个字符的通配符, "~" 表示模糊搜索,"*" 表示多个字符的通配符。

GET account_info/_search
{
  "query": {
    "query_string": {
      "default_field": "email",
      "query": "qqcom~",
      "fuzziness": 1
    }
  }
}

        可以指定模糊搜索的最大编辑距离。上面的查询将在文档中搜索与 "qqcom~" 关键词的拼写相似且最大编辑距离为 1的文档。

4.4 指定搜索字段和搜索条件

GET grade2/_search
{
  "query": {
    "query_string": {
      "fields": ["name", "age"],
      "default_operator": "AND", 
      "query": "name:刘一, age:26"
    }
  },
  "track_total_hits": true
}
GET grade2/_search
{
  "query": {
    "query_string": {
      "fields": ["name", "age"],
      "default_operator": "AND", 
      "query": "name:刘一 age:26"
    }
  },
  "track_total_hits": true
}
GET grade2/_search
{
  "query": {
    "query_string": {
      "fields": ["name", "age"],
      "default_operator": "AND", 
      "query": "name:刘一 AND age:26"
    }
  },
  "track_total_hits": true
}

GET grade2/_search
{
  "query": {
    "query_string": {
      "fields": ["name", "age"],
      "default_operator": "AND", 
      "query": "name:刘一 AND age:[25 TO 26]"
    }
  },
  "track_total_hits": true
}

        上面的查询将在 name 和 age 字段中搜索包含关键词 "刘一" 并且年龄在 25 到 26 之间的文档,其中 fields 参数用于指定搜索字段,default_operator 参数用于指定默认的逻辑操作符。

        需要注意的是,query_string 查询可能存在安全风险,因为它允许直接执行用户输入的查询字符串,可能导致潜在的搜索注入攻击,因此在使用时需谨慎验证和过滤用户输入,以防止安全漏洞。同时,根据实际需求和场景,可以根据 Elasticsearch 的文档和查询语法进行更多的配置和优化。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值