2013-08-26 77 views
3

我正在使用前綴查詢彈性搜索查詢。在頂級數據上使用它時,它可以正常工作,但是一旦應用於嵌套數據,就不會返回任何結果。我嘗試查詢數據如下所示:Elasticsearch前綴查詢不適用於嵌套文檔

這裏前綴查詢工作正常: 查詢:

{ "query": { "prefix" : { "duration": "7"} } } 

結果:

{ 
    "took": 25, ... }, 
    "hits": { 
     "total": 6, 
     "max_score": 1, 
     "hits": [ 
     { 
     "_index": "itemresults", 
     "_type": "itemresult", 
     "_id": "ITEM_RESULT_7c8649c2-6cb0-487e-bb3c-c4bf0ad28a90_8bce0a3f-f951-4a01-94b5-b55dea1a2752_7c965241-ad0a-4a83-a400-0be84daab0a9_61", 
     "_score": 1, 
     "_source": { 
      "score": 1, 
      "studentId": "61", 
      "timestamp": 1377399320017, 
      "groupIdentifiers": {}, 
      "assessmentItemId": "7c965241-ad0a-4a83-a400-0be84daab0a9", 
      "answered": true, 
      "duration": "7.078", 
      "metadata": { 
       "Korrektur": "a", 
       "Matrize12_13": "MA.1.B.1.d.1", 
       "Kompetenz": "ZuV", 
       "Zyklus": "Z2", 
       "Schwierigkeit": "H", 
       "Handlungsaspekt": "AuE", 
       "Fach": "MA", 
       "Aufgabentyp": "L" 
      }, 
      "assessmentSessionId": "7c8649c2-6cb0-487e-bb3c-c4bf0ad28a90", 
      "assessmentId": "8bce0a3f-f951-4a01-94b5-b55dea1a2752" 
     } 
    }, 

現在試圖用前綴查詢應用於嵌套結構「元數據」不返回任何結果:

{ "query": { "prefix" : { "metadata.Fach": "M"} } } 

結果:

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

我在做什麼錯?是否可以在嵌套數據上應用前綴

回答

6

這並不取決於是否嵌套。這取決於你的映射,如果你正在索引時分析字符串。

我打算把一個例子:

我創建和指數具有以下映射:

curl -XPUT 'http://localhost:9200/test/' -d ' 
{ 
    "mappings": { 

    "test" : { 
     "properties" : { 
     "text_1" : { 
      "type" : "string", 
      "index" : "analyzed" 
     }, 
     "text_2" : { 
      "index": "not_analyzed", 
      "type" : "string" 
     } 
     } 
    } 
    } 
}' 

基本上2個文本字段,一個分析和其他not_analyzed。我現在指數下列文件:

curl -XPUT 'http://localhost:9200/test/test/1' -d ' 
{ 
"text_1" : "Hello world", 
"text_2" : "Hello world" 
}' 

文本1查詢

由於文字_1分析的是elasticsearch做的事情之一是到外地轉換成小寫。所以,如果我做下面的查詢沒有找到任何文件:

curl -XGET 'http://localhost:9200/test/test/_search?pretty=true' -d ' 
{ "query": { "prefix" : { "text_1": "H"} } } 
' 
{ 
    "took" : 2, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 0, 
    "max_score" : null, 
    "hits" : [ ] 
    } 
} 

但是,如果我做的伎倆,並使用小寫作出查詢:

curl -XGET 'http://localhost:9200/test/test/_search?pretty=true' -d ' 
{ "query": { "prefix" : { "text_1": "h"} } } 
' 
{ 
    "took" : 2, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "test", 
     "_type" : "test", 
     "_id" : "1", 
     "_score" : 1.0, "_source" : 
{ 
"text_1" : "Hello world", 
"text_2" : "Hello world" 
} 
    } ] 
    } 
} 

_2查詢

由於text_2未被分析,所以當我將原始查詢與它匹配時:

curl -XGET 'http://localhost:9200/test/test/_search?pretty=true' -d ' 
{ "query": { "prefix" : { "text_2": "H"} } } 
' 
{ 
    "took" : 1, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "test", 
     "_type" : "test", 
     "_id" : "1", 
     "_score" : 1.0, "_source" : 
{ 
"text_1" : "Hello world", 
"text_2" : "Hello world" 
} 
    } ] 
    } 
}