弹性搜索获取的数组中的项目是否满足要求?

时间:2016-10-13 02:32:19

标签: elasticsearch

我是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,它将被返回。我想让那些应用程序中的项目符合需要。有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:1)

我用regex暂时解决这个问题。 发布http://127.0.0.1:9200/index/type/_search

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

不得包含那些applications.jobs.country包含" aa"

以外的内容