Query String & Simple Query String

2019-07-10 11:06:57 浏览数 (1)

1、向搜索库中添加数据

代码语言:javascript复制
PUT /users/_doc/1
{
  "name":"Ruan Yiming",
  "about":"java, golang, node, swift, elasticsearch"
}

PUT /users/_doc/2
{
  "name":"Li Yiming",
  "about":"Hadoop"
}

2、使用query_string 查询数据

代码语言:javascript复制
POST users/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "Ruan AND Yiming"
    }
  }
}


POST users/_search
{
  "query": {
    "query_string": {
      "fields":["name","about"],
      "query": "(Ruan AND Yiming) OR (Java AND Elasticsearch)"
    }
  }
}

3、使用simple_query_string查询数据

代码语言:javascript复制
#Simple Query 默认的operator是 Or
代码语言:javascript复制
POST users/_search
{
  "query": {
    "simple_query_string": {
      "query": "Ruan AND Yiming",
      "fields": ["name"]
    }
  }
}
    上面这个查询和这个带default_operator 查询结果的一样的  默认的查询连接词是OR 对比query_string查询我们可知
这里query的and不作为连接词只用  其实应为 Ruan OR AND OR Yiming
POST users/_search
{
  "query": {
    "simple_query_string": {
      "query": "Ruan AND Yiming",
      "fields": ["name"],
      "default_operator": "OR"
    }
  }
}
如果要是用AND查询则需要设置default_operator 属性为AND
POST users/_search
{
  "query": {
    "simple_query_string": {
      "query": "Ruan Yiming",
      "fields": ["name"],
      "default_operator": "AND"
    }
  }
}

0 人点赞