2015-10-29 29 views
0

我正在使用Meteor(如Javascript,Node,NPM等),並希望爲用戶提供簡單的文本輸入,以便通過Elasticsearch進行搜索。我希望能夠在+和「」這樣的文本上使用修飾符並搜索特定的字段。我正在尋找可以將純文本輸入轉換爲Elasticsearch Query DSL的東西。使用字符串爲Elasticsearch構建查詢DSL

這將是一些示例查詢:

該查詢將意味着關鍵字「塔圖因」必須存在:

stormtrooper +tatooine 

這意味着「死亡之星」應該是一個關鍵詞:

stormtrooper "death star" 

這隻會在類別字段中搜索關鍵字「花絮」:

stormtrooper category=bloopers 

有沒有可以做到這一點的圖書館?是否可以有一個通用的解決方案,或者這就是爲什麼我找不到任何現有的答案?

回答

0

simple_query_string會支持你的查詢語法開箱即用,除了category=bloopers這應該是category:bloopers代替,但除此之外,它應該工作:

curl -XPOST localhost:9200/your_index/_search -d '{ 
    "query": { 
    "simple_query_string": { 
     "query": "stormtrooper category:bloopers" 
    } 
    } 
}' 

curl -XPOST localhost:9200/your_index/_search -d '{ 
    "query": { 
    "simple_query_string": { 
     "query": "stormtrooper +tatooine" 
    } 
    } 
}' 

您還可以發送查詢的query string直接像這樣:

curl -XPOST localhost:9200/your_index/_search?q=stormtrooper%20%22death%20star%22"