参考博客:https://www.cnblogs.com/ddcoder/articles/7457082.html
POST 00000_niupi_object_entity_ren/_doc/1
{
"xingming": [
{
"source": "手工录入",
"value": "涯哥"
},
{
"source": "手工录入",
"value": "阿涯古"
}
],
"school": [
{
"source": "建模导入",
"value": "中山大学"
}
]
}
POST 00000_niupi_object_entity_ren/_doc/2
{
"xingming": [
{
"source": "手工录入",
"value": "粉仔"
},
{
"source": "手工录入",
"value": "阿宁古"
}
],
"school": [
{
"source": "建模导入",
"value": "广州商学院"
}
]
}
POST 00000_niupi_object_entity_ren/_doc/3
{
"xingming": [
{
"source": "建模导入",
"value": "梁非凡"
}
],
"school": [
{
"source": "建模导入",
"value": "广州商学院"
}
]
}
POST 00000_niupi_object_entity_ren/_doc/4
{
"xingming": [
{
"source": "建模导入",
"value": "张三"
}
],
"from": [
{
"source": "建模导入",
"value": "北京"
}
]
}
”query”:”nam\\*:obama”
有AND、OR,默认为OR。比如query里面的内容是”cat dog”,两个短语以空格分开,如果default_operator参数为OR,那么只要字段内包含cat或者dog之一就可以匹配。
如果default_operator为AND,字段内必须同时包含cat和dog才可以匹配。与bool查询挺像的。
指定查询时使用的分词器
比如query:”cat dog mouse”,这个配置项为2,那么只有字段里至少同时包含这三个中的任意两个才会匹配。
需要注意的是,这个配置项只对default_operator为OR的时候生效。
如果这个是AND,那么cat dog mouse必须全部包含无论minimum_should_match为多少。
# 普通高亮查询
# xingming.value:建 school.value:广州商学院
# 条件和条件默认是should的关系;并且对条件的值是match,会进行分词
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"query_string": {
"query": "xingming.value:建 school.value:广州商学院"
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
# 即等价于下面
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"xingming.value": "建"
}
},
{
"match": {
"school.value": "广州商学院"
}
}
]
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
运算符:+表示must,-表示must_not,不写表示should
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"query_string": {
"query": "+xingming.value:古惑仔 +school.value:广州商学院"
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
GET 00000_niupi_object_entity_ren/_search
{
"explain": false,
"query": {
"query_string": {
"query": "school.value:\"广州商学院\""
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
# 等价于下面
# 怎么证明等价于下面?开explain,然后对比两个语句的explain结果,是一模一样的
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"match_phrase": {
"school.value": "广州商学院"
}
},
"explain": true
}
GET 00000_niupi_object_entity_ren/_search
{
"explain": false,
"query": {
"query_string": {
"query": "+school.value:\"广州商学院\" and +xingming.value:\"梁非凡\""
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"xingming.value": "梁非凡"
}
},
{
"match_phrase": {
"school.value": "广州商学院"
}
}
]
}
},
"explain": false
}
GET 00000_niupi_object_entity_ren/_search
{
"explain": false,
"query": {
"query_string": {
"query": "school.value:\"广州商学院\" and +xingming.value:\"梁非凡\""
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"xingming.value": "梁非凡"
}
}
],
"should": [
{
"match_phrase": {
"school.value": "广州商学院"
}
}
]
}
},
"explain": false
}
GET 00000_niupi_object_entity_ren/_search
{
"explain": false,
"query": {
"query_string": {
"query": "school.value:\"广州商学院\" and xingming.value:\"梁非凡\""
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"xingming.value": "梁非凡"
}
},
{
"match_phrase": {
"school.value": "广州商学院"
}
}
]
}
},
"explain": false
}
GET 00000_niupi_object_entity_ren/_search
{
"explain": false,
"query": {
"query_string": {
"query": "(+school.value:\"广州商学院\") or (+xingming.value:\"涯哥\")"
}
},
"highlight": {
"fields": {
"*.value": {}
}
}
}
GET 00000_niupi_object_entity_ren/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match_phrase": {
"school.value": "广州商学院"
}
}
]
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"xingming.value": "涯哥"
}
}
]
}
}
]
}
},
"explain": false
}
可以看出,and是条件在同一个bool里面;or是2个条件在2个bool里,最后这2个bool在包在一个bool+should里