1. 程式人生 > >Elasticsearch學習筆記(二) 重要的Query DSL

Elasticsearch學習筆記(二) 重要的Query DSL

0. 準備好資料

"hits": [
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "鹽城鼕鼕",
          "age": 30,
          "hometown": "鹽城",
          "gender": "male",
          "interesting": "watching TV"
        }
, "sort": [ "1" ] }, { "_index": "pigg", "_type": "_doc", "_id": "2", "_score": null, "_source": { "name": "珣爺", "age": 28, "hometown": "徐州", "gender": "female", "interesting"
: "watching movie" }, "sort": [ "2" ] }, { "_index": "pigg", "_type": "_doc", "_id": "3", "_score": null, "_source": { "name": "米可", "age": 1, "hometown": "蘇州", "gender": "female"
}, "sort": [ "3" ] } ]

1. 查詢部分欄位

GET /pigg/_search
{
  "_source": ["name", "age"]
}

2. match

#查詢interesting匹配"watching TV"
GET /pigg/_search
{
  "query": {
    "match": {
      "interesting": "watching  TV"
    }
  }
}

返回如下:

"hits": [
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "name": "鹽城鼕鼕",
          "age": 30,
          "hometown": "鹽城",
          "gender": "male",
          "interesting": "watching TV"
        }
      },
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "珣爺",
          "age": 28,
          "hometown": "徐州",
          "gender": "female",
          "interesting": "watching movie"
        }
      }
    ]

看到結果也返回了"interesting"= "watching movie"的資料, 其中id=1的_score要比id=2的要高,
這個說明是匹配的程度,id=1的要比id=2的更加匹配

#查詢interesting匹配"TV"或者"moive"
GET /pigg/_search
{
  "query": {
    "match": {
      "interesting": "TV movie"
    }
  }
}

返回結構如下:

"hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "珣爺",
          "age": 28,
          "hometown": "徐州",
          "gender": "femal",
          "interesting": "watching movie"
        }
      },
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "鹽城鼕鼕",
          "age": 30,
          "hometown": "鹽城",
          "gender": "male",
          "interesting": "watching TV"
        }
      }
    ]

上面結果命中了2個人, "_score"都是0.2876821,說明匹配度兩者相同

#查詢age=30GET /pigg/_search
{
  "query": {
    "match": {
      "age": 30
    }
  }
}

3. match_phrase

#短語查詢,這個會將"watching TV"作為一個短語去進行匹配查詢
GET /pigg/_search
{
  "query": {
    "match_phrase": {
      "interesting": "watching TV"
    }
  }
}

返回結果如下:

"hits": [
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "name": "鹽城鼕鼕",
          "age": 30,
          "hometown": "鹽城",
          "gender": "male",
          "interesting": "watching TV"
        }
      }
    ]

4.must

查詢interesting匹配"watching TV",並且gender匹配"female"

GET /pigg/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "interesting": "watching TV" }},
        { "match": {"gender": "female" }}
      ]
    }
  }
}

返回結果如下:

    "hits": [
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "珣爺",
          "age": 28,
          "hometown": "徐州",
          "gender": "female",
          "interesting": "watching movie"
        }
      }
    ]

{ “match”: { “interesting”: “watching TV” }}這條件語句能返回id=1或2的資料
{ “match”: {“gender”: “female” }}這條件語句能返回id=2或3的資料
這兩條語句是且的關係,所有最後返回id=2的資料

5. should

5.1查詢interesting匹配"watching mobile",gender匹配"female"

#查詢interesting匹配"watching mobile",或gender匹配"female"
GET /pigg/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "interesting": "watching mobile" }},
        { "match": {"gender": "female" }}
      ]
    }
  }
}

返回結果如下:

"hits": [
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "珣爺",
          "age": 28,
          "hometown": "徐州",
          "gender": "female",
          "interesting": "watching movie"
        }
      },
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "鹽城鼕鼕",
          "age": 30,
          "hometown": "鹽城",
          "gender": "male",
          "interesting": "watching TV"
        }
      },
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "name": "米可",
          "age": 1,
          "hometown": "蘇州",
          "gender": "female"
        }
      }
    ]

從上面結果看,id=2的資料匹配得分最高,另外兩個匹配度相同
注意這次查詢的是"watching mobile",不是"watching TV"

5.2 minimum_should_match

這個是指或的條件,必須滿足多少條,下面的minimum_should_match=2,所以一條都查不到

GET /pigg/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 3
            }
          }
        },
        {
         "match": { "hometown.keyword": "徐州" }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

6. must_not

查詢interesting不匹配 “watching TV”,
並且
gender不匹配 “female”

GET /pigg/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": {"interesting": "watching movie"} },
        { "match": {"gender": "female"} }
      ]
    }
  }
}

查詢結果是空的,沒有匹配的資料

7. 組合多查詢

用SQL表示如下
where gender != ‘male’ and ( (age >= 0 and age <= 3) or hometown = ‘徐州’ )

GET /pigg/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "gender": "male"
          }
        }
      ],
      "should": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 3
            }
          }
        },
        {
         "match": {
           "hometown.keyword": "徐州"
         }
        }
      ]
    }
  }
}

返回結果如下:

"hits": [
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "米可",
          "age": 1,
          "hometown": "蘇州",
          "gender": "female"
        }
      },
      {
        "_index": "pigg",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "珣爺",
          "age": 28,
          "hometown": "徐州",
          "gender": "female",
          "interesting": "watching movie"
        }
      }
    ]

8.不影響評分的filter

如果不希望age的比較影響評分,可以放到filter裡

GET /pigg/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {"interesting": "watching TV"}
        }
      ], 
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 29
          }
        }
      }
    }
  }
}