ElasticSearch 搜索方式

请求地址:
GET http://localhost:9200/blogindex/blog/_search

一、match

{
    
    
  "query": {
    
    
    "match": {
    
    
        "name" : "中华人民共和国"
    }
  }
}

上面的查询会先对内容进行分词,分词以后所有包含这分词中的一个或多个的文档就会被搜索出来。并且根据lucene的评分机制(TF/IDF)来进行评分。

二、match_phrase

{
    
    
  "query": {
    
    
    "match_phrase": {
    
    
        "name" : "中华人民共和国"
    }
  }
}

精确匹配所有同时包含所有分词的文档

完全匹配可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用到slop。

{
    
    
    "query": {
    
    
        "match_phrase": {
    
    
            "name": {
    
    
                "query": "中华人民共和国",
                "slop":  1
            }
        }
    }
}

三、multi_match

{
    
    
  "query": {
    
    
    "multi_match": {
    
    
        "query" : "中华人民共和国",
        "fields" : ["title", "content"]
    }
  }
}

如果我们希望两个字段进行匹配,其中一个字段匹配分词,文档就满足,使用multi_match

type
best_fields

完全匹配的文档占的评分比较高,则需要使用best_fields,

{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "中华人民共和国",
      "type": "best_fields",
      "fields": [
        "tag",
        "content"
      ],
      "tie_breaker": 0.3
    }
  }

most_fields

希望越多字段匹配的文档评分越高,就要使用most_fields

{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "中华人民共和国",
      "type": "most_fields",
      "fields": [
        "tag",
        "content"
      ]
    }
  }

cross_fields

希望这个词条的分词词汇是分配到不同字段中的,那么就使用cross_fields

{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "中华人民共和国",
      "type": "cross_fields",
      "fields": [
        "tag",
        "content"
      ]
    }
  }

四、term

term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

{
    
    
  "query": {
    
    
    "term": {
    
    
        "name" : "中华人民共和国"
    }
  }
}

五、bool

联合查询,多条件匹配

must: 文档必须完全匹配条件
should: should下面会带一个以上的条件,至少满足一个条件,这个文档就符合should
must_not: 文档必须不匹配条件

{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": {
    
    
        "term": {
    
    
           "name" : "中华人民共和国"
        }
      },
      "must_not": {
    
    
        "term": {
    
    
          "title" : "中华人民共和国"
        }
      }
    }
  }
}

六、match_all

全部搜索

{
    
    
  "query": {
    
    
    "match_all": {
    
    
        "name" : "中华人民共和国"
    }
  }
}

七、Query Filter

可以对搜索结果进行过滤

{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
           "name" : "中华人民共和国"
          }
        }
      ], 
      "filter": {
    
    
        "range": {
    
    
          "price": {
    
    
            "gt": 4000
          }
        }
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/u014553029/article/details/111992750