elasticsearch之基本查詢

來自: https://segmentfault.com/a/1190000004428116

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

基本查詢種類

term查詢

{ 
    "query": {
        "term": {
            "title": "crime"
        }
    }
}
  • 指定權重

  • </ul>

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

    • </ul>

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

      常用詞查詢

      簡單理解就是去除停用詞的高權限,分高低頻兩組去查詢,像停用詞就是高頻的,cutoff_frequency表示低于這個概率的詞將出現在低頻組中。

      { 
          "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匹配文本的分詞

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

      短語查詢

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

      前綴查詢

      對查詢關鍵詞的最后一個詞條做前綴匹配

      { 
          "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指對于給定的關鍵詞,只有最高分才會包括在最后的文檔的評分中,而不是所有包含該詞條的所有字段分數之和。

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

      simple_query_string查詢

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

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

      標識符查詢

      使用唯一表示uid來說查找

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

      前綴查詢

      前綴匹配給定的關鍵詞

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

      • </ul>

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

        fuzzy查詢

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

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

        • </ul>

          { 
              "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"
                          }
                      }
                  }
              }
          }

          參考

 本文由用戶 SilasSorrel 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!