【问题标题】:How to search data within object using elasticsearch 7+ java API如何使用elasticsearch 7+ java API在对象内搜索数据
【发布时间】:2020-02-14 06:32:45
【问题描述】:

我想搜索所有 role_id 为 8 且 status.id 为 5 的文档。我的数据有以下索引。

我尝试通过以下查询仅通过 role_id 为 8 获取数据

{
    "query": {
      "bool": {
        "filter": [
          {  "match" : {"role.role_id" : 8} }

        ]
      }
    }
}

它返回 "hits": [] 不确定这里出了什么问题。

我是弹性搜索的新手,我看到很多文档都提到了对象的嵌套类型。这是强制性的吗?我需要更改索引吗?

{
    "contacts": {
        "aliases": {},
        "mappings": {
            "properties": {
                 "roles": {
                    "properties": {
                        "role_id": {
                            "type": "long"
                        },
                        "role_name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "status": {
                            "properties": {
                                "id": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "name": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这应该可以解决问题:

    curl -XGET 'localhost:9200/contacts/_search?q=roles.role_id:8%20AND%20roles.status.id:5'
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多