2016-10-13 78 views
0

我是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

我用正則表達式來臨時解決這個問題。 POST http://127.0.0.1:9200/index/type/_search

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

不得包含applications.jobs.country包含比 「AA」

其他