2012-09-24 85 views
3

我有,看起來像彈性搜索文檔......elasticsearch短語檢索

{ 
    "items": 
    [ 
     "ONE BLAH BLAH BLAH TWO BLAH BLAH BLAH THREE", 
     "FOUR BLAH BLAH BLAH FIVE BLAH BLAH BLAH SIX" 
    ] 
} 

我希望能夠搜索該文檔與短語查詢像...

{ 
    "match_phrase" : { 
     "items" : "ONE TWO THREE" 
    } 
} 

這樣它就會匹配而不管中間的單詞。這些詞也需要按照這個順序。我意識到這可以通過slop屬性來實現,但是當我正在試驗時,如果斜坡不僅僅是我在搜索的內容之間的話,它似乎會被包裝起來,因爲這是我不確定的詞數量我認爲斜坡是合適的。此外,我需要的數組中的每個項目只搜索,所以......

{ 
    "match_phrase" : { 
     "items" : "ONE TWO SIX" 
    } 
} 

慣於比賽本文檔SIX是在陣列中ONETWO不同的項目。

所以我的問題是,這是可能通過elasticsearch或將我必須做一個對象數組和使用嵌套查詢來搜索它們?

回答

11

它可以使用Span Near Query完成。我不確定您的實驗中出了什麼問題,以及「包裝」的含義。我只能猜測,也許你指定了「in_order」:「false」,你的查詢只是忽略了術語的順序。你能提供一個例子嗎?

爲了避免查詢跨越多個項目,您需要使用「position_offset_gap」屬性增加映射中項目之間的「間距」。這裏是一個例子:

curl -XDELETE "localhost:9200/slop-test" 
echo 
curl -XPUT "localhost:9200/slop-test" -d '{ 
    "settings" : { 
    "index" : { 
     "number_of_shards" : 1, 
     "number_of_replicas" : 0 
    }  
    }, 
    "mappings" : { 
    "doc" : { 
     "properties" : { 
     "items" : { 
      "type" : "string", 
      "position_offset_gap": 100 
     } 
     } 
    } 
    } 
}' 
echo 
curl -XPUT "localhost:9200/slop-test/doc/1" -d '{ 
    "items": 
    [ 
     "ONE BLAH BLAH BLAH TWO BLAH BLAH BLAH THREE", 
     "FOUR BLAH BLAH BLAH FIVE BLAH BLAH BLAH SIX" 
    ] 
}' 
curl -XPOST "localhost:9200/slop-test/_refresh" 
echo 
curl "localhost:9200/slop-test/_search?pretty=true" -d '{ 
    "query" : { 
    "span_near" : { 
     "clauses" : [ 
     { "span_term" : { "items" : "one" } }, 
     { "span_term" : { "items" : "two" } }, 
     { "span_term" : { "items" : "three" } } 
     ], 
     "slop" : 40, 
     "in_order" : true 
    } 
    } 
}' 
echo 
curl "localhost:9200/slop-test/_search?pretty=true" -d '{ 
    "query" : { 
    "span_near" : { 
     "clauses" : [ 
     { "span_term" : { "items" : "one" } }, 
     { "span_term" : { "items" : "two" } }, 
     { "span_term" : { "items" : "six" } } 
     ], 
     "slop" : 40, 
     "in_order" : true 
    } 
    } 
}' 
echo