Elasticsearchノート(二)Query DSLの最も重要なクエリー
46603 ワード
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"
}
}
]
結果を見ると、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=30
GET /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のデータを返すことができます
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のデータマッチングの得点が最も高く、他の2つのマッチング度が同じであることに注意今回のクエリは「watching mobile」であり、「watching TV」ではない
5.2 minimum_should_match
これは、次のminimum_の条件を満たす必要があります.should_match=2なので1本も調べられません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.採点に影響しないフィルタ
ageの比較が採点に影響しないようにするには、filterに入れます.GET /pigg/_search
{
"query": {
"bool": {
"must": [
{
"match": {"interesting": "watching TV"}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 29
}
}
}
}
}
}
"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"
]
}
]
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"
}
}
]
結果を見ると、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=30
GET /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のデータを返すことができます
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のデータマッチングの得点が最も高く、他の2つのマッチング度が同じであることに注意今回のクエリは「watching mobile」であり、「watching TV」ではない
5.2 minimum_should_match
これは、次のminimum_の条件を満たす必要があります.should_match=2なので1本も調べられません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.採点に影響しないフィルタ
ageの比較が採点に影響しないようにするには、filterに入れます.GET /pigg/_search
{
"query": {
"bool": {
"must": [
{
"match": {"interesting": "watching TV"}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 29
}
}
}
}
}
}
# 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 "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"
}
}
]
# age=30
GET /pigg/_search
{
"query": {
"match": {
"age": 30
}
}
}
# , "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のデータを返すことができます
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のデータマッチングの得点が最も高く、他の2つのマッチング度が同じであることに注意今回のクエリは「watching mobile」であり、「watching TV」ではない
5.2 minimum_should_match
これは、次のminimum_の条件を満たす必要があります.should_match=2なので1本も調べられません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.採点に影響しないフィルタ
ageの比較が採点に影響しないようにするには、filterに入れます.GET /pigg/_search
{
"query": {
"bool": {
"must": [
{
"match": {"interesting": "watching TV"}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 29
}
}
}
}
}
}
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"
}
}
]
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のデータマッチングの得点が最も高く、他の2つのマッチング度が同じであることに注意今回のクエリは「watching mobile」であり、「watching TV」ではない
5.2 minimum_should_match
これは、次のminimum_の条件を満たす必要があります.should_match=2なので1本も調べられません
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.採点に影響しないフィルタ
ageの比較が採点に影響しないようにするには、filterに入れます.GET /pigg/_search
{
"query": {
"bool": {
"must": [
{
"match": {"interesting": "watching TV"}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 29
}
}
}
}
}
}
GET /pigg/_search
{
"query": {
"bool": {
"must_not": [
{ "match": {"interesting": "watching movie"} },
{ "match": {"gender": "female"} }
]
}
}
}
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.採点に影響しないフィルタ
ageの比較が採点に影響しないようにするには、filterに入れます.GET /pigg/_search
{
"query": {
"bool": {
"must": [
{
"match": {"interesting": "watching TV"}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 29
}
}
}
}
}
}
GET /pigg/_search
{
"query": {
"bool": {
"must": [
{
"match": {"interesting": "watching TV"}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 29
}
}
}
}
}
}