【elasticsearch】kibana-ES的布尔运算

1.布尔运算之 AND 的使用 

--查询 city='武汉' and "teacher"= "王麻子"
GET sspuuser/_search
{
"query": {
"bool": {
	"must": [
	{
		"match": {
		"teacher": "王麻子"
		}
	},
	{
		"match": {
		"city": "武汉"
		}
	}
	]
}
}
}
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.933259,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 1.933259,
        "_source" : {
          "id" : 7,
          "name" : "sspu007",
          "city" : "武汉",
          "course" : "redis",
          "teacher" : "王麻子",
          "birthdate" : "20200210"
        }
      }
    ]
  }
}

--如果是三个条件呢
--查询 city='武汉' and "teacher"= "王麻子" and course='redis'
GET sspuuser/_search
{
"query": {
"bool": {
	"must": [
	{
		"match": {
		"teacher": "王麻子"
		}
	},
	{
		"match": {
		"city": "武汉"
		}
	},
	{
		"match": {
		 "city" : "武汉"
		}
	}
	]
}
}
}

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.3195531,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 3.3195531,
        "_source" : {
          "id" : 7,
          "name" : "sspu007",
          "city" : "武汉",
          "course" : "redis",
          "teacher" : "王麻子",
          "birthdate" : "20200210"
        }
      }
    ]
  }
}

2.es布尔运算之

should 查询:OR 查询;
--"name"="sspu007" or "course" = "开源数据库"
GET sspuuser/_search
{
"query": {
"bool": {
"should": [
	{"match": {"name":"sspu007"}},
	{"match": {"course": "开源数据库"}}
    ]
}
}
}
--如果是关系数据库,我们应该可以查询到两条。
--但是这里查询除了4条。
--course=开源数据库,应该是做了拆解,匹配到了"数据库"关键字,
--所以都被查询出来了。
{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 2.7230785,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 2.7230785,
        "_source" : {
          "id" : 5,
          "name" : "sspu005",
          "city" : "杭州",
          "course" : "开源数据库",
          "teacher" : "王麻子",
          "birthdate" : "20200827"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 2.407774,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 2.238372,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 0.6931471,
        "_source" : {
          "id" : 7,
          "name" : "sspu007",
          "city" : "武汉",
          "course" : "redis",
          "teacher" : "王麻子",
          "birthdate" : "20200210"
        }
      }
    ]
  }
}

3.布尔运算之排查

--查询course不等于:'国产数据库'
GET sspuuser/_search
{
	"query": {
	"bool": {
	"must_not": [
	{"match": {"course" : "国产数据库"}}
	]
	}
	}
}
--包含:"数据库关键字的都被过滤掉了"
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 0.0,
        "_source" : {
          "id" : 7,
          "name" : "sspu007",
          "city" : "武汉",
          "course" : "redis",
          "teacher" : "王麻子",
          "birthdate" : "20200210"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "id" : 2,
          "name" : "sspu002",
          "city" : "深圳",
          "course" : "mysql",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200826"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "id" : 3,
          "name" : "sspu003",
          "city" : "北京",
          "course" : "sqlserver",
          "teacher" : "王麻子 1号",
          "birthdate" : "20200825"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.0,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "id" : 1,
          "name" : "sspu001",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "王麻子",
          "birthdate" : "20200829"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.0,
        "_source" : {
          "id" : 6,
          "name" : "sspu006",
          "city" : "成都",
          "course" : "greenplum",
          "teacher" : "王麻子",
          "birthdate" : "20200729"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 0.0,
        "_source" : {
          "id" : 8,
          "name" : "sspu008",
          "city" : "北京",
          "course" : "mongodb",
          "teacher" : "王麻子",
          "birthdate" : "20190529"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 0.0,
        "_source" : {
          "id" : 9,
          "name" : "sspu009",
          "city" : "上海",
          "course" : "db2",
          "teacher" : "王麻子",
          "birthdate" : "20180229"
        }
      }
    ]
  }
}
--同时不满足多个条件的过滤掉。
GET sspuuser/_search
{
	"query": {
	"bool": {
	"must_not": [
	{"match": {"course" : "国产数据库"}},
	{"match": {"course" : "db2"}},
	{"match": {"course" : "mongodb"}},
	{"match": {"course" : "sqlserver"}},
	{"match": {"course" : "redis"}},
	{"match": {"course" : "mysql"}}
	]
	}
	}
}
--不满足的过滤后,结果如下。
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.0,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "id" : 1,
          "name" : "sspu001",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "王麻子",
          "birthdate" : "20200829"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.0,
        "_source" : {
          "id" : 6,
          "name" : "sspu006",
          "city" : "成都",
          "course" : "greenplum",
          "teacher" : "王麻子",
          "birthdate" : "20200729"
        }
      }
    ]
  }
}

4.满足部分条件,同时不满足某些条件=xx, 同时<>YY;

--"teacher"= "王麻子 1 号"
--查询teacher中包含1的值。
GET sspuuser/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"teacher": "1"
}
}
]}}}
--结果如下。
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.6931471,
        "_source" : {
          "id" : 3,
          "name" : "sspu003",
          "city" : "北京",
          "course" : "sqlserver",
          "teacher" : "王麻子 1号",
          "birthdate" : "20200825"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 0.6931471,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      }
    ]
  }
}

--teacher包含1,时间范围过滤:生日大于:20200701

GET sspuuser/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "teacher": "1"
          }
        }
      ],
      "filter": {
        "range": {
          "birthdate": {
            "gt": "20200825"
          }
        }
      }
    }
  }
}

{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.6931471,
        "_source" : {
          "id" : 3,
          "name" : "sspu003",
          "city" : "北京",
          "course" : "sqlserver",
          "teacher" : "王麻子 1号",
          "birthdate" : "20200825"
        }
      }
    ]
  }
}

gt:大于 :>
gte:大于等于:>=
lt:小于:<
lte:小于等于:<=
--生日小于:20200824的文档。
GET sspuuser/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "teacher": "1"
          }
        }
      ],
      "filter": {
        "range": {
          "birthdate": {
            "lt": "20200824"
          }
        }
      }
    }
  }
}
--小于等于:20200302的文档。

GET sspuuser/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "teacher": "1"
          }
        }
      ],
      "filter": {
        "range": {
          "birthdate": {
            "lte": "20200302"
          }
        }
      }
    }
  }
}
--结果如下。
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 0.6931471,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      }
    ]
  }
}

5.按照日期范围查询 

GET sspuuser/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "birthdate": {
            "gte": 20200302,
            "lte": 20200824
          }
        }
      }
    }
  }
}
--结果如下。
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 0.0,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.0,
        "_source" : {
          "id" : 6,
          "name" : "sspu006",
          "city" : "成都",
          "course" : "greenplum",
          "teacher" : "王麻子",
          "birthdate" : "20200729"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 0.0,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      }
    ]
  }
}

6.模糊匹配 

--查询包含:"数据库" 的记录,
GET sspuuser/_search
{
  "query": {
    "match": {
      "course": "数据库"
    }
  }
}
--结果。
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 2.407774,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 2.407774,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 2.238372,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.633847,
        "_source" : {
          "id" : 5,
          "name" : "sspu005",
          "city" : "杭州",
          "course" : "开源数据库",
          "teacher" : "王麻子",
          "birthdate" : "20200827"
        }
      }
    ]
  }
}
--搜索城市中包含"上"的文档

GET sspuuser/_search
{
  "query": {
    "match": {
      "city": "上"
    }
  }
}
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.2039728,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 0.87546873,
        "_source" : {
          "id" : 9,
          "name" : "sspu009",
          "city" : "上海",
          "course" : "db2",
          "teacher" : "王麻子",
          "birthdate" : "20180229"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 0.87546873,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      }
    ]
  }
}

--搜索城市中包含"上"或者包含"广"的文档
GET sspuuser/_search
{
  "query": {
    "match": {
      "city": "上广"
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.3862942,
        "_source" : {
          "id" : 1,
          "name" : "sspu001",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "王麻子",
          "birthdate" : "20200829"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.2039728,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.2039728,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 0.87546873,
        "_source" : {
          "id" : 9,
          "name" : "sspu009",
          "city" : "上海",
          "course" : "db2",
          "teacher" : "王麻子",
          "birthdate" : "20180229"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 0.87546873,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      }
    ]
  }
}

--搜索包含"上海"的文档。
GET sspuuser/_search
{
  "query": {
    "match": {
      "city": "上海"
    }
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 2.4079456,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 2.4079456,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.7509375,
        "_source" : {
          "id" : 9,
          "name" : "sspu009",
          "city" : "上海",
          "course" : "db2",
          "teacher" : "王麻子",
          "birthdate" : "20180229"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 1.7509375,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      }
    ]
  }
}

7.match_phrase

完整匹配指定的值:返回包含"上海"的记录。
GET sspuuser/_search
{
  "query": {
    "match_phrase": {
      "city": "上海"
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 2.4079456,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 2.4079456,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.7509375,
        "_source" : {
          "id" : 9,
          "name" : "sspu009",
          "city" : "上海",
          "course" : "db2",
          "teacher" : "王麻子",
          "birthdate" : "20180229"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 1.7509375,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      }
    ]
  }
}
--搜索包含:"上广"的记录,没有记录。这个和上面的 match 得到的结果不同
GET sspuuser/_search
{
  "query": {
    "match_phrase": {
      "city": "上广"
    }
  }
}
--搜索包含:"广" 的文档
GET sspuuser/_search
{
  "query": {
    "match_phrase": {
      "city": "广"
    }
  }
}
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.3862942,
        "_source" : {
          "id" : 1,
          "name" : "sspu001",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "王麻子",
          "birthdate" : "20200829"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.2039728,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      }
    ]
  }
}

8.term查询,精确匹配查询 

--精确匹配。
--搜索:city 包含 "上" 的文档,没有结果。
GET sspuuser/_search
{
  "query": {
    "term": {
      "city": "上"
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.2039728,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 0.87546873,
        "_source" : {
          "id" : 9,
          "name" : "sspu009",
          "city" : "上海",
          "course" : "db2",
          "teacher" : "王麻子",
          "birthdate" : "20180229"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 0.87546873,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      }
    ]
  }
}

--搜索:city="上海" 的文档,没有查询到结果。
GET sspuuser/_search
{
  "query": {
    "term": {
      "city": "上海"
    }
  }
}

--term和match的区别。
--match是经过分析(analyze)的,会根据分词结果进行匹配。
--term是不经过分词的,直接取倒排索引查找精确的值。

--不推荐使用:term,而是使用 match;

GET sspuuser/_search
{
  "query": {
    "term": {
      "name": "sspu011"
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 1.3862942,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        }
      }
    ]
  }
}

9.高亮显示 

--查询出city包含"广"的稳定,city中"广"高亮显示。
GET sspuuser/_search
{
	"query":{
	"match": {
	"city" : "广"
	}
	},
	"highlight" :{
	"fields": {
	"city":{}
	}
	}
}
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.3862942,
        "_source" : {
          "id" : 1,
          "name" : "sspu001",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "王麻子",
          "birthdate" : "20200829"
        },
        "highlight" : {
          "city" : [
            "<em>广</em>州"
          ]
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.2039728,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        },
        "highlight" : {
          "city" : [
            "<em>广</em>州"
          ]
        }
      }
    ]
  }
}
--"广" 高亮显示
curl -XGET 192.168.1.7:9201/sspuuser/_search?pretty -H 'Content-Type: application/json' -d '
{
	"query":{
	"match": {
	"city" : "广"
	}
	},
	"highlight" :{
	"fields": {
	"city":{}
	}
	}
}
'

10.自定义高亮显示。

--teacher高亮显示为红色。
GET sspuuser/_search
{
	"query":{
	"match": {
	"name" : "sspu010"
	}
	},
	"highlight" :{
	"pre_tags": "<b class='key' style='color:red'>",
	"post_tags": "</b>",
	"fields": {
	"teacher":{}
	}
	}
}
--结果中未出现高亮显示的标签。
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.2039728,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      }
    ]
  }
}

--'teacher'高亮显示
curl -XGET 192.168.1.7:9201/sspuuser/_search?pretty -H 'Content-Type: application/json' -d '
{
	"query":{
	"match": {
	"name" : "sspu010"
	}
	},
	"highlight" :{
	"pre_tags": "<b class='key' style='color:red'>",
	"post_tags": "</b>",
	"fields": {
	"teacher":{}
	}
	}
}'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值