2014-11-01 109 views
2

我是elasticseach的新手,我有一些問題需要在我的查詢中添加過濾器。在grails中用彈性搜索查詢嵌套字段

我的領域類是這樣的:

Class A { 
    String name 
    A_Status status 

    static searchable = { 
     status component: true 
    } 
} 

Class AImpl extends A { 

} 

Class A_Status { 
    String name 
    static searchable = { 
     root : false 
     only = 'name' 
    } 
} 

在我控制我做的查詢是:

def res = elasticSearchService.search() 
{ 
    bool { 
     must { 
      term("status.name": "ACTIVE") 
     } 
    } 
} 

我試圖改變searchableAImpl或將"searchable = true",但有相同的結果,查詢總是空的,它應該得到4個結果。

我發現的另一個奇怪的事情是,做uri搜索給了我預期的結果,但是身體查詢卻沒有。

curl -XGET 'http://localhost:9200/com.sisconline.entities/_search?q=status.name=ACTIVE' 

這有4個匹配。

curl -XPOST 'http://localhost:9200/com.sisconline.entities/_search' -d '{ 
"query" : { 
     "term":{ "status.name":"ACTIVE"} 
     } 
}' 

這會得到0個命中。我使用Grails 2.3.4elasticsearch plugin 0.0.3.5

問候。

回答

2

我終於做來解決問題:

must { 
    nested { 
    path = "status" 
    query { 
     bool { 
      must { 
       term("status.name": "active") } 
      } 
     } 
    } 
} 

希望它可以幫助別人。

+0

您是否嘗試使用您的域進行搜索? 我有類似的映射(甚至幾乎相同),並面臨的問題,當我嘗試搜索AImpl,fe 'AImpl.search(「$ {query}」)'有一個例外在tomcat 'ERROR unmarshall.DomainClassUnmarshaller - 使用ID解組類AImpl時出錯... 消息:屬性AImpl.status未映射爲[組件],但找到了打破的搜索命中。 ' 另外,如果我嘗試通過超進行搜索,像 'A.search(「$ {查詢}」)' 我接收不到任何結果,似乎什麼也沒有編入索引的A. – 2014-11-07 20:50:31

+0

@JulyAntonicheva從我到目前爲止,我們已經看到框架將索引子類而不是父類,所以如果您嘗試通過父類進行搜索,您將無法獲得任何結果。實際上,我必須在我的所有子類中編寫可搜索的閉包,也許有一個我沒有看到的配置參數。我沒有查詢使用我的域名,因爲我想要所有可能的結果。 – santi2332 2014-11-08 22:49:30

+0

@JulyAntonicheva關於你遇到的錯誤,可能是你沒有指定你的屬性作爲一個組件,在我的例子中是「status component:true」,或者你改變了你的映射並且沒有清理你的索引來重建它。 – santi2332 2014-11-08 22:54:52

相關問題