zoukankan      html  css  js  c++  java
  • 【ElasticSearch(七)进阶】multi_match多字段匹配、bool复合查询

    【ElasticSearch(七)进阶】multi_match多字段匹配,bool复合查询


    一、multi_match多字段匹配

    例:查询 address 和 city 中任意一项包含 mill urie的结果

    GET /bank/_search
    {
      "query":{
        "multi_match": {
          "query": "mill urie",
          "fields": ["address","city"]
        }
      }
    }
    

    返回结果:

    我们发现multi_match也会进行语句的分词,再评分。

    {
      "took" : 27,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : 6.5059485,
        "hits" : [
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "136",
            "_score" : 6.5059485,
            "_source" : {
              "account_number" : 136,
              "balance" : 45801,
              "firstname" : "Winnie",
              "lastname" : "Holland",
              "age" : 38,
              "gender" : "M",
              "address" : "198 Mill Lane",
              "employer" : "Neteria",
              "email" : "winnieholland@neteria.com",
              "city" : "Urie",
              "state" : "IL"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "970",
            "_score" : 5.4032025,
            "_source" : {
              "account_number" : 970,
              "balance" : 19648,
              "firstname" : "Forbes",
              "lastname" : "Wallace",
              "age" : 28,
              "gender" : "M",
              "address" : "990 Mill Road",
              "employer" : "Pheast",
              "email" : "forbeswallace@pheast.com",
              "city" : "Lopezo",
              "state" : "AK"
            }
          },
         。。。
        ]
      }
    }
    

    二、bool复合查询

    如果我们面对更加复杂的查询条件需要采用 bool 复合查询

    例如:

    查询 gender 是 M、address 包含 mill、年龄不能是28、lastname最好是Wallace的结果:

    should:最好是,如果查不到也可以不是。匹配到的,得分高。

    GET /bank/_search
    {
      "query":{
        "bool":{
          "must": [
            {"match": {
               "gender": "M"
            }},
            {"match":{
              "address": "mill"
            }}
          ],
          "must_not": [
            {"match":{
              "age": "18"
            }}
          ],
          "should":[
            {"match":{
              "lastname": "Wallace"
            }}
          ]
        }
      }
    }
    

    返回:

    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 3,
          "relation" : "eq"
        },
        "max_score" : 12.585751,
        "hits" : [
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "970",
            "_score" : 12.585751,
            "_source" : {
              "account_number" : 970,
              "balance" : 19648,
              "firstname" : "Forbes",
              "lastname" : "Wallace",
              "age" : 28,
              "gender" : "M",
              "address" : "990 Mill Road",
              "employer" : "Pheast",
              "email" : "forbeswallace@pheast.com",
              "city" : "Lopezo",
              "state" : "AK"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "136",
            "_score" : 6.0824604,
            "_source" : {
              "account_number" : 136,
              "balance" : 45801,
              "firstname" : "Winnie",
              "lastname" : "Holland",
              "age" : 38,
              "gender" : "M",
              "address" : "198 Mill Lane",
              "employer" : "Neteria",
              "email" : "winnieholland@neteria.com",
              "city" : "Urie",
              "state" : "IL"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "345",
            "_score" : 6.0824604,
            "_source" : {
              "account_number" : 345,
              "balance" : 9812,
              "firstname" : "Parker",
              "lastname" : "Hines",
              "age" : 38,
              "gender" : "M",
              "address" : "715 Mill Avenue",
              "employer" : "Baluba",
              "email" : "parkerhines@baluba.com",
              "city" : "Blackgum",
              "state" : "KY"
            }
          }
        ]
      }
    }
    
  • 相关阅读:
    velocity语法
    使用VS2003创建WEB程序的时候出现"AutoMation服务器不能创建对象"错误
    ASP.NET 2.0 Tips:跨页提交
    Tip #1 – 创建、管理、应用样式表的强大工具(Visual Studio 2008)
    解决ASP.NET2.0和1.1在同一台电脑上不能并行的问题
    Tip #2 - 样式应用工具(style application toolbar)
    利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆
    ASP.NET Tips: 获取插入记录的ID
    通过rsync远程增量备份数据
    array_merge() [function.arraymerge]: Argument #1 is not an array in ……错误的解决办法
  • 原文地址:https://www.cnblogs.com/musecho/p/15179989.html
Copyright © 2011-2022 走看看