你可以在這裏做幾件事。
首先,我建立了一個沒有任何明確映射或分析的索引,這意味着將使用standard analyzer。這很重要,因爲它決定了我們如何根據文本字段進行查詢。
於是我開始:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
PUT /test_index/doc/1
{
"date": "2013-12-30T00:00:00.000Z",
"value": 2,
"dimensions": {
"region": "Coimbra District"
}
}
PUT /test_index/doc/2
{
"date": "2013-12-30T00:00:00.000Z",
"value": 1,
"dimensions": {
"region": "Federal District"
}
}
PUT /test_index/doc/3
{
"date": "2013-12-30T00:00:00.000Z",
"value": 1,
"dimensions": {
"region": "Masovian Voivodeship"
}
}
然後我想你的查詢,並沒有得到命中。我不明白你爲什麼在你fields
參數有"dimensions.ga:region"
,但是當我把它改爲"dimensions.region"
我得到了一些結果:
POST /test_index/doc/_search
{
"query": {
"query_string": {
"query": "Masovian Voivodeship OR Federal District",
"fields": [
"dimensions.region"
]
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.46911472,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_score": 0.46911472,
"_source": {
"date": "2013-12-30T00:00:00.000Z",
"value": 1,
"dimensions": {
"region": "Masovian Voivodeship"
}
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 0.3533006,
"_source": {
"date": "2013-12-30T00:00:00.000Z",
"value": 1,
"dimensions": {
"region": "Federal District"
}
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.05937162,
"_source": {
"date": "2013-12-30T00:00:00.000Z",
"value": 2,
"dimensions": {
"region": "Coimbra District"
}
}
}
]
}
}
然而,這將返回你不希望的結果。要解決這個問題的方法之一是如下:
POST /test_index/doc/_search
{
"query": {
"query_string": {
"query": "(Masovian AND Voivodeship) OR (Federal AND District)",
"fields": [
"dimensions.region"
]
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.46911472,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_score": 0.46911472,
"_source": {
"date": "2013-12-30T00:00:00.000Z",
"value": 1,
"dimensions": {
"region": "Masovian Voivodeship"
}
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 0.3533006,
"_source": {
"date": "2013-12-30T00:00:00.000Z",
"value": 1,
"dimensions": {
"region": "Federal District"
}
}
}
]
}
}
另一種方式做到這一點(我喜歡這個更好),這也是同樣的結果是使用match query和boolean should組合:
POST /test_index/doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"dimensions.region": {
"query": "Masovian Voivodeship",
"operator": "and"
}
}
},
{
"match": {
"dimensions.region": {
"query": "Federal District",
"operator": "and"
}
}
}
]
}
}
}
這裏是我使用的代碼:
http://sense.qbox.io/gist/bb5062a635c4f9519a411fdd3c8540eae8bdfd51
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string- query.html嘗試將default_operator設置爲AND。或者讓你的查詢「Masovian和Voivodeship或聯邦和區」 – 2015-02-09 17:54:14
嗨,我試着用查詢'{ 「query_string」:{ 「query」:「Masovian和Voivodeship OR Federal and District」, 「fields」:[ dimensions.region「] } }'但它沒有返回任何命中。 – 2015-02-09 18:39:49