elasticsearch利用term实现同时具备和任意均可的条件查询

elasticsearch利用term实现同时具备和任意均可的条件查询


#设置映射

PUT /test
{
    "settings": {"index.refresh_interval": "1s"},
    "mappings": {
    "properties": {
        "id": {
            "type": "long"
        },
        "name": {
            "type": "keyword"
        }
    }
}
}

添加几条测试数据后,即可通过以下方法实现需求


#同时具备

GET /test/_search
{
    "query": {
    "bool": {
        "must": [
            {
              "term": {
                    "name": "AA"
                }
            },
                        {
              "term": {
                    "name": BBB"
                }
            },
                        {
              "term": {
                    "name": "CCC"
                }
            },
            {
              "term": {
                    "name": "DDD"
                }
            }
        ]
    }
}
}


#任意均可

GET /test/_search
{
    "query": {
    "bool": {
        "must": [
            {
              "terms": {
                    "name": ["AAA","BBB"]
                }
            }
        ]
    }
}
}


鼎云博客
  • 最新评论
  • 总共0条评论