天天看点

Elasticsearch multi_match 多字段查询

作者:贵哥说Java创业

多字段查询,比如查询 name 和 desc 字段包含单词 的 的文档。

# 多字段查询
GET /index_name/_search
{
    "query":{
        "multi_match":{
            "query":"的",
            "fields":[
                "name",
                "desc"
            ]
        }
    }
}

# 输出结果
{
    "took":25,
    "timed_out":false,
    "_shards":{
        "total":2,
        "successful":2,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":{
            "value":4,
            "relation":"eq"
        },
        "max_score":0.33698124,
        "hits":[
            {
                "_index":"index_name",
                "_type":"_doc",
                "_id":"1",
                "_score":0.33698124,
                "_source":{
                    "id":1001,
                    "name":"张三",
                    "age":12,
                    "desc":"我的自我描述",
                    "birthday":"2020-02-02"
                }
            },
            {
                "_index":"index_name",
                "_type":"_doc",
                "_id":"G2cyrYYBhncLYm9cmrSc",
                "_score":0.33698124,
                "_source":{
                    "id":1003,
                    "name":"王五",
                    "age":14,
                    "desc":"我的自我述3",
                    "birthday":"2023-02-03"
                }
            },
            {
                "_index":"index_name",
                "_type":"_doc",
                "_id":"KWcyrYYBhncLYm9cWbGB",
                "_score":0.31387398,
                "_source":{
                    "id":1002,
                    "name":"李四",
                    "age":13,
                    "desc":"我的自我描2述",
                    "birthday":"2023-02-03"
                }
            },
            {
                "_index":"index_name",
                "_type":"_doc",
                "_id":"Rmc5rYYBhncLYm9c_fxA",
                "_score":0.2876821,
                "_source":{
                    "id":1005,
                    "name":"王五",
                    "age":14,
                    "desc":"超人的平凡人生",
                    "birthday":"2023-02-04"
                }
            }
        ]
    }
}           

继续阅读