ElasticSearch API query_string

参考博客: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_string API

查询表达式 query

  • query:查询表达式

要查询的字段 default_field、fields

  • default_field:要查询的字段(单个,默认是_all,即对所有字段进行查询)
  • fields:要查询的多个字段(例如:fields:[“age”, “name”],fields中只要有一个字段满足query的条件即可匹配查询)
  1. default_field和fields只选其中一个
  2. fields支持一些简单的wildcard写法。比如fields:[“nam*”]即任何nam开头的字段,注意:如果field与query写在一起比如”query”:”name:obama”,要使用wildcard需要转义
”query”:”nam\\*:obama”
  1. 当fields有多个的时候,会有另外一个参数use_dis_max
    true代表使用dis_max查询,false为bool查询
    至于use_dis_max的用法:请参考http://blog.csdn.NET/dm_vincent/article/details/41820537

默认运算符 default_operator

 有AND、OR,默认为OR。比如query里面的内容是”cat dog”,两个短语以空格分开,如果default_operator参数为OR,那么只要字段内包含cat或者dog之一就可以匹配。
 如果default_operator为AND,字段内必须同时包含cat和dog才可以匹配。与bool查询挺像的。

分词器 analyzer

指定查询时使用的分词器

最小匹配词条 minimum_should_match

 比如query:”cat dog mouse”,这个配置项为2,那么只有字段里至少同时包含这三个中的任意两个才会匹配。
需要注意的是,这个配置项只对default_operator为OR的时候生效
 如果这个是AND,那么cat dog mouse必须全部包含无论minimum_should_match为多少。

多条件查询

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 + match

运算符:+表示must,-表示must_not,不写表示should

GET 00000_niupi_object_entity_ren/_search
{
  "query": {
    "query_string": {
      "query": "+xingming.value:古惑仔 +school.value:广州商学院"
    }
  },
  "highlight": {
    "fields": {
      "*.value": {}
    }
  }
}

match_pharse

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
}

布尔运算符

and和must、should的关系

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
}

or和must、should的关系

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里

你可能感兴趣的:(ELK,elasticsearch,大数据,搜索引擎)