elasticsearch之基本查詢

本文主要記錄es的基本查詢api的使用html

基本查詢種類

term查詢

{ 
    "query": {
        "term": {
            "title": "crime"
        }
    }
}
  • 指定權重正則表達式

{ 
    "query": {
        "term": {
            "title": {
                "value":"crime",
                "boost":10.0
             }
        }
    }
}
  • 多term查詢
    查詢tags中包含novel或bookjson

{ 
    "query": {
        "terms": {
            "tags": ["novel","book"]
        }
    }
}

經常使用詞查詢

簡單理解就是去除停用詞的高權限,分高低頻兩組去查詢,像停用詞就是高頻的,cutoff_frequency表示低於這個機率的詞將出如今低頻組中。api

{ 
    "query": {
        "common": {
             "title":{
                 "query":"crime and punishment",
                 "cutoff_frequency":0.001
             }
        }
    }
}

match查詢(不支持lucene查詢語法)

查詢title包含crime或and或punishment的文檔

{ 
    "query": {
        "match": {
            "title": "crime and punishment"
        }
    }
}

operator操做符

要求and或者or匹配文本的分詞elasticsearch

{ 
    "query": {
        "match": {
            "title": {
                 "query":"crime and punishment",
                 "operator":"and"
            }
        }
    }
}

短語查詢

{ 
    "query": {
        "match_phrase": {
            "title": {
                 "query":"crime  punishment",
                 "slop":1
            }
        }
    }
}

前綴查詢

對查詢關鍵詞的最後一個詞條作前綴匹配ide

{ 
    "query": {
        "match_phrase_prefix": {
            "title": {
                 "query":"crime  punish",
                 "slop":1,
                 "max_expansions":20
            }
        }
    }
}

multi_match(針對多個字段查詢)

{ 
    "query": {
        "multi_match": {
             "query":"crime  heller",
             "fields":["title","author"]
        }
    }
}

query_string查詢(支持lucene的查詢語法)

title字段包含crime,且權重爲10,也要包含punishment,可是otitle不包含cat,同事author字段包含Fyodor和dostoevsky。性能

{ 
    "query": {
        "query_string": {
             "query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)",
             "default_field":"title"
        }
    }
}

針對多字段查詢

use_dis_max使用最大分查詢,max指對於給定的關鍵詞,只有最高分纔會包括在最後的文檔的評分中,而不是全部包含該詞條的全部字段分數之和。ui

{ 
    "query": {
        "query_string": {
             "query":"crime heller",
             "fields":["title","author"],
              "use_dis_max":true
        }
    }
}

simple_query_string查詢

解析出錯時不拋異常,丟棄查詢無效的部分code

{ 
    "query": {
        "simple_query_string": {
             "query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)",
             "default_operator":"or"
        }
    }
}

標識符查詢

使用惟一表示uid來講查找regexp

{ 
    "query": {
        "ids": {
             "type":"book",
             "values":["1","2","3"]
        }
    }
}

前綴查詢

前綴匹配給定的關鍵詞

{ 
    "query": {
        "prefix": {
             "title":"cri"
        }
    }
}
  • 指定權重

{ 
    "query": {
        "prefix": {
             "title":{
                 "value":"cri",
                 "boost":3.0
             }
        }
    }
}

fuzzy查詢

使用編輯距離的模糊查詢,計算量較大,可是對用戶拼寫錯的場景比較有用

{ 
    "query": {
        "fuzzy": {
             "title":"crme"
        }
    }
}
  • 指定最小類似度誤差

{ 
    "query": {
        "fuzzy": {
             "title":{
                 "value":"crme",
                 "min_similarity":1
              }
        }
    }
}

通配符查詢

支持*和?等通配符

{ 
    "query": {
        "wildcard": {
             "title": "cr?me"
        }
    }
}

範圍查詢

只能針對單個字段,能夠是數值型的,也能夠是基於字符串的。

{ 
    "query": {
        "range": {
             "year": {
                  "gte" :1890,
                  "lte":1900
              }
        }
    }
}

正則表達式查詢

查詢性能取決於正則表達式

{ 
    "query": {
        "regexp": {
             "title": {
                  "value" :"cr.m[ae]",
                  "boost":10.0
              }
        }
    }
}

布爾查詢(組合查詢)

{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "title": "crime"
                }
            }, 
            "should": {
                "range": {
                    "year": {
                        "from": 1900, 
                        "to": 2000
                    }
                }
            }, 
            "must_not": {
                "term": {
                    "otitle": "nothing"
                }
            }
        }
    }
}

參考

相關文章
相關標籤/搜索