2015-10-21 149 views
2

我的過濾查詢存在問題。當我運行以下查詢:使用術語過濾器彈性搜索過濾查詢

{ 
"query":{ 
    "filtered":{ 
    "query":{ 
     "regexp":{ 
      "name":"chri.*" 
     } 
    }, 
    "filter":{ 
     "bool":{ 
      "must":[ 
       {"term": { "accountNonLocked": "false" }} 
      ] 
     } 
    } 
    } 
},"fields" : ["name", "id", "accountNonLocked","role"], 
"from" : 0, 
"size" : 10 
} 

我得到如下回應

{ 
"took": 4, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 1, 
    "hits": [ 
    { 
    "_index": "candidatindex", 
    "_type": "job", 
    "_id": "54c7867ecfcbbe42dc000478", 
    "_score": 1, 
    "fields": { 
     "name": [ 
     "Christophe toto" 
     ], 
     "accountNonLocked": [ 
     true 
     ], 
     "role": [ 
     "DHR" 
     ] 
    } 
    }, 
    { 
    "_index": "candidatindex", 
    "_type": "job", 
    "_id": "54c7867ecfcbbe42dc000468", 
    "_score": 1, 
    "fields": { 
     "name": [ 
     "Christophe Toto" 
     ], 
     "accountNonLocked": [ 
     true 
     ], 
     "role": [ 
     "CANDIDATE" 
     ] 
    } 
    } 
    ]} 
} 

但是,當我的角色這樣添加過濾器:

{ 
"query":{ 
    "filtered":{ 
    "query":{ 
     "regexp":{ 
      "name":"chri.*" 
     } 
    }, 
    "filter":{ 
     "bool":{ 
      "must":[ 
       {"term": { "accountNonLocked": "false" }}, 
       {"term": { "role": "CANDIDATE" }} 
      ] 
     } 
    } 
    } 
},"fields" : ["name", "id", "accountNonLocked","role"], 
"from" : 0, 
"size" : 10 
} 

然後,我有一個空結果:

{ 
"took": 3, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 0, 
    "max_score": null, 
    "hits": [] 
    } 
} 

是否有任何e可以告訴我我做錯了什麼?

回答

2

您的role字段可能是analyzed字符串。 因此,無論以小寫值嘗試像

{"term": { "role": "candidate" }} 

或更改role領域的映射是not_analyzed(你需要重新索引數據的更改生效)

+0

這是它非常感謝你。 –

+0

太棒了,很高興它解決了。 – Val

+0

感謝您的信息。這是我在這個網站上的第一個問題。 –