elasticsearch中不同的搜索类型之间的区别

先插入5条数据

{
    "_index": "students",
    "_id": "1",
    "_score": 1.6013623,
    "_source": {
        "name": "王阳明",
        "age": 28,
        "description": "哈哈哈 你好啊"
    }
},
{
    "_index": "students",
    "_id": "4",
    "_score": 1.284406,
    "_source": {
        "name": "阳明",
        "age": 88,
        "description": "哈哈哈 123456 你好啊"
    }
},
{
    "_index": "students",
    "_id": "5",
    "_score": 0.9128574,
    "_source": {
        "name": "王阳",
        "age": 58,
        "description": "哈哈哈 123 你好啊"
    }
},
{
    "_index": "students",
    "_id": "2",
    "_score": 0.5070823,
    "_source": {
        "name": "王二小",
        "age": 18,
        "description": "嘿嘿嘿 他好啊"
    }
},
{
    "_index": "students",
    "_id": "3",
    "_score": 0.2706483,
    "_source": {
        "name": "李阳才",
        "age": 55,
        "description": "呼呼呼 我好啊"
    }
}

match轻量级搜索

{
    "query": {
        "match": {
            "name": "王阳明"
        }
    }
}

上面的查询就会进行分词,比如"王阳明"就会被分为:"王阳明" ,“阳明”, “王”,“阳”,“明”,
那么所有包含这些词当中的一个或多个的文档就会被搜索出来,并会根据lucene的评分机制(TF/IDF)进行评分

match_phrase短语搜索

{
    "query": {
        "match_phrase": {
            "name": "王阳明"
        }
    }
}

match_phrase要求只匹配上"王阳明"这个短语,完全匹配可能比较严

multi_match多字段匹配

如果我们希望两个或者两个以上的字段进行匹配,其中一个字段能匹配上就满足的话,使用multi_match

  • 注意:fields字段列表中,不允许写不同类型的字段,比如把age字段也写入进去的话,会报错
{
    "query": {
        "multi_match": {
            "query": "王阳明",
            "fields": [
                "name",
                "description"
            ]
        }
    }
}

multi_match中有三种类型:best_fields、most_fields、cross_fields(最佳字段、多数字段、跨字段)
(1)我们希望完全匹配的文档占的评分比较高,则需要使用best_fields,multi_match默认是best_fields
(2)我们希望越多字段匹配的文档评分越高,就要使用most_fields
(3)我们会希望这个词条的分词词汇是分配到不同字段中的,那么就使用cross_fields,

term精确查找

{
    "query": {
        "term": {
            "name.keyword": {
                "value": "王阳明"
            }
        }
    }
}

与match查询类似,但term是精确查找,代表完全匹配,即不进行分词器分析

posted @ 2022-04-13 14:23  专职  阅读(64)  评论(0编辑  收藏  举报