【问题标题】:Elastic Search get items in array filed that all meet to requirements?Elasticsearch 在数组字段中获取所有符合要求的项目?
【发布时间】:2016-10-13 02:32:19
【问题描述】:

我是 Elastic Search 的新手,现在被一个问题阻止了。

如果我使用GET http://127.0.0.1:9200/index/type/_search,结果是

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "index",
        "_type": "type",
        "_id": "AVe3AcpYbUIbMIPUYcZ2",
        "_score": 1,
        "_source": {
          "applications": [
            {
              "jobs": [
                {
                  "id": 1,
                  "country": "a"
                },
                {
                  "id": 2,
                  "country": "b"
                }
              ]
            },
            {
              "jobs": [
                {
                  "id": 3,
                  "country": "c"
                },
                {
                  "id": 4,
                  "country": "d"
                }
              ]
            }
          ]
        }
      },
      {
        "_index": "index",
        "_type": "type",
        "_id": "AVe3cMaPbUIbMIPUYcaS",
        "_score": 1,
        "_source": {
          "applications": [
            {
              "jobs": [
                {
                  "id": 11,
                  "country": "aa"
                },
                {
                  "id": 2,
                  "country": "aa"
                }
              ]
            },
            {
              "jobs": [
                {
                  "id": 33,
                  "country": "aa"
                },
                {
                  "id": 44,
                  "country": "aa"
                }
              ]
            }
          ]
        }
      },
      {
        "_index": "index",
        "_type": "type",
        "_id": "AVe3b1KmbUIbMIPUYcaR",
        "_score": 1,
        "_source": {
          "applications": [
            {
              "jobs": [
                {
                  "id": 11,
                  "country": "aa"
                },
                {
                  "id": 2,
                  "country": "bb"
                }
              ]
            },
            {
              "jobs": [
                {
                  "id": 33,
                  "country": "cc"
                },
                {
                  "id": 44,
                  "country": "d"
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

我想要的是获得一份申请,申请中的每个job.country都是“aa”,所以预期的结果是

"applications": [
            {
              "jobs": [
                {
                  "id": 11,
                  "country": "aa"
                },
                {
                  "id": 2,
                  "country": "aa"
                }
              ]
            },
            {
              "jobs": [
                {
                  "id": 33,
                  "country": "aa"
                },
                {
                  "id": 44,
                  "country": "aa"
                }
              ]
            }
          ]

我尝试的是 POST http://127.0.0.1:9200/index/type/_search

{
    "query": {
        "match": {
            "applications.jobs.country": "aa"
        }
    }
}

但是结果是

{
  "took": 65,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.30685282,
    "hits": [
      {
        "_index": "index",
        "_type": "type",
        "_id": "AVe3cMaPbUIbMIPUYcaS",
        "_score": 0.30685282,
        "_source": {
          "applications": [
            {
              "jobs": [
                {
                  "id": 11,
                  "country": "aa"
                },
                {
                  "id": 2,
                  "country": "aa"
                }
              ]
            },
            {
              "jobs": [
                {
                  "id": 33,
                  "country": "aa"
                },
                {
                  "id": 44,
                  "country": "aa"
                }
              ]
            }
          ]
        }
      },
      {
        "_index": "index",
        "_type": "type",
        "_id": "AVe3b1KmbUIbMIPUYcaR",
        "_score": 0.15342641,
        "_source": {
          "applications": [
            {
              "jobs": [
                {
                  "id": 11,
                  "country": "aa"
                },
                {
                  "id": 2,
                  "country": "bb"
                }
              ]
            },
            {
              "jobs": [
                {
                  "id": 33,
                  "country": "cc"
                },
                {
                  "id": 44,
                  "country": "d"
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

似乎如果应用程序包含符合要求的applications.jobs.country,它将被返回。我想让applications.jobs中的每一项都满足需要。有人可以帮助我吗?谢谢

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我使用正则表达式来临时解决这个问题。 发布http://127.0.0.1:9200/index/type/_search

    {
        "query": {
            "bool": {
                "must_not": {
                    "regexp": {
                        "applications.jobs.country": "~(.*aa.*)"
                    }
                }
            }
        }
    }
    

    不得包含 applications.jobs.country 包含“aa”以外的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      相关资源
      最近更新 更多