ES语法使用

bool?

query?

match?

should?

must?

与其他一起使用......

ES 全面查询语法_mon_star的博客-CSDN博客_es查询语法

term : term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型,也就是没有含ik分词器的字段),如果字段含有ik分词器的一定拼接 . (此处有个点)keyword进行精确查询

//请求参数
{
    "from": 0,
    "size": 100,
    "query": {
         "term":{"companyName":"中国台湾 Bonart Co.,Ltd."}
    }
}
//结果
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}
//原因term会直接对关键词进行查找,不进行分词,一般用于精确查询,所以如果字段含有ik分词器的一定拼接.keyword进行精确查询
//正确的请求参数
{
    "from": 0,
    "size": 100,
    "query": {
         "term":{"companyName.keyword":"中国台湾 Bonart Co.,Ltd."}
    }
}
//返回结果
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 9.618911,
        "hits": [
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016815",
                "_score": 9.618911,
                "_source": {
                    "companyCode": "X00026",
                    "companyName": "中国台湾 Bonart Co.,Ltd.",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016815",
                    "companyAddress": "台北县新庄市五权一路3号4楼之114F-11,No.3,Wuchuan1st.Rd.,Hsinchuang,TaipeiHsien,Taiwan",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            }
        ]
    }
}

bool不能直接与term使用

//错误示范
{
    "from": 0,
    "size": 100,
    "query": {
        "bool": {
           "term":{"companyName":"中国台湾 Bonart Co.,Ltd."}
        }
    }
}
//结果
{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[bool] query does not support [term]",
                "line": 6,
                "col": 19
            }
        ],
        "type": "parsing_exception",
        "reason": "[bool] query does not support [term]",
        "line": 6,
        "col": 19
    },
    "status": 400
}
//正确参数  只用于精确查询,含ik分词器的需要拼接.keyword,不含ik的不用拼接 否则无数据
{
    "from": 0,
    "size": 100,
    "query": {
        "bool": {
          "should":[
              { "term":{"companyName.keyword":"中国台湾 Bonart Co.,Ltd."}}
          ]
        }
    }
}
//结果
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 9.618911,
        "hits": [
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016815",
                "_score": 9.618911,
                "_source": {
                    "companyCode": "X00026",
                    "companyName": "中国台湾 Bonart Co.,Ltd.",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016815",
                    "companyAddress": "台北县新庄市五权一路3号4楼之114F-11,No.3,Wuchuan1st.Rd.,Hsinchuang,TaipeiHsien,Taiwan",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            }
        ]
    }
}
//结论:bool不能直接与term使用

 //terms 不能跟其他的一起使用?

terms 用于一个字段多个值 精确查询  如果字段含有ik分词器的一定拼接.keyword进行精确查询 不含不用拼接

{
    "from": 0,
    "size": 100,
    "query": {
      "terms": { 
        "companyName.keyword": [ "中国台湾 Bonart Co.,Ltd.","中国台湾 华广生技股份有限公司BIONIME CORPORATION"] 
        }
    }
}
//结果
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016815",
                "_score": 1.0,
                "_source": {
                    "companyCode": "X00026",
                    "companyName": "中国台湾 Bonart Co.,Ltd.",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016815",
                    "companyAddress": "台北县新庄市五权一路3号4楼之114F-11,No.3,Wuchuan1st.Rd.,Hsinchuang,TaipeiHsien,Taiwan",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            },
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016826",
                "_score": 1.0,
                "_source": {
                    "companyCode": "X00037",
                    "companyName": "中国台湾 华广生技股份有限公司BIONIME CORPORATION",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016826",
                    "companyAddress": "台中县大里市仁化路694号 №694, Renhua Road, Dali City, Taichung County, Taiwan",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            }
        ]
    }
}

match用于字段分词  ,must相当于 and 。 must-match 用于一个字段多个值时 表示查询出的这个字段中需要包含多个值中经过分词之后的值,相当于 字段 like( 值1 值2 ) ,也可用于多个字段模糊查询相当于 and 字段1 likeXXX  and 字段2 like XXX。

{
    "from": 0,
    "size": 100,
    "query": {
       "bool": {
      "must": [
        {
          "match": {
          "companyName": "康"
          }
        },
           {
          "match": {
          "companyName": "中国台湾 Bonart Co.,Ltd."
          }
        }
      ]
    }
    }
}
//结果
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 8.031464,
        "hits": [
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016881",
                "_score": 8.031464,
                "_source": {
                    "companyCode": "X00092",
                    "companyName": "中国台湾优你康光学有限公司",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016881",
                    "companyAddress": "台湾台北市南港路3段130巷3弄11号2楼",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            },
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016845",
                "_score": 7.685155,
                "_source": {
                    "companyCode": "X00056",
                    "companyName": "中国台湾 康扬股份有限公司",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016845",
                    "companyAddress": "嘉义县民雄乡丰收村大学路二段2363号",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            },
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016821",
                "_score": 7.0750175,
                "_source": {
                    "companyCode": "X00032",
                    "companyName": "中国台湾 优你康光学股份有限公司",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016821",
                    "companyAddress": "台湾新竹县湖口乡中正村中正路2段196号",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            }
        ]
    }
}
//companyName字段值中包含了中国台湾和康这两个查询值中的分词

//must-match 也可用于精确查询,由于must是and关系所以只能是一个字段一个值, 含ik分词器的字段需要拼接 .keyword  相当于 字段1=值1 and 字段2 = 值2

{
    "from": 0,
    "size": 100,
    "query": {
       "bool": {
      "must": [
        {
          "match": {
          "companyName.keyword": "General Life Biotechnology Co.,Ltd."
          }
        }
      ]
    }
    }
}
//或者  在确定某个文章也就是相当于数据库中的某条数据中某个字段的值时可以这样精确查询
{
    "from": 0,
    "size": 100,
    "query": {
       "bool": {
      "must": [
        {
          "match": {
          "produceId": "1000016826"
          }
        },
         {
          "match": {
          "companyName.keyword": "中国台湾 华广生技股份有限公司BIONIME CORPORATION"
          }
        }
      ]
    }
    }
}
//结果
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 19.237822,
        "hits": [
            {
                "_index": "world_enterprise",
                "_type": "_doc",
                "_id": "1000016826",
                "_score": 19.237822,
                "_source": {
                    "companyCode": "X00037",
                    "companyName": "中国台湾 华广生技股份有限公司BIONIME CORPORATION",
                    "createCompanyId": "-1",
                    "remark": "初始化",
                    "dr": "0",
                    "productCategory": "2",
                    "createdAt": "2020-04-27 10:58:02",
                    "produceId": "1000016826",
                    "companyAddress": "台中县大里市仁化路694号 №694, Renhua Road, Dali City, Taichung County, Taiwan",
                    "territorialId": "189",
                    "dataSource": "1",
                    "updatedAt": "2020-04-27 10:58:02"
                }
            }
        ]
    }
}

含有ik分词器的字段拼接.keyword的意思是字段=值 不拼接时表示字段包含值

当只查询一个字段时使用should和must没区别 先说模糊  也就是含有ik分词器的字段不拼接.keyword 时使用term (不对值进行分词) 意思是字段包含 值

使用match(对值进行分词) 意思是字段包含分词后的值中的一种词就可以

//match
{
    "from": 0,
    "size": 100,
    "query": {
       "bool": {
      "must": [      
         {
          "match": {
          "companyName": "中国 台湾" //match 进行分词 分成 中国  空格 台湾 中国台湾 或者单个字
          }
        }
      ]
    }
    }
}
//term
{
    "from": 0,
    "size": 100,
    "query": {
       "bool": {
      "must": [      
         {
          "term": {
          "companyName": "中国 台湾" //term不对字段值进行分词 而是说companyName包含中国 台湾
          }
        }
      ]
    }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值