2013-07-30 73 views
6

我對ES相當陌生,正在將它用於我的一個新項目。從一開始,我爲客戶提供了一個簡單的映射,其中包含姓名和付款信息對象列表。如果我在SQL中執行此操作,它將像客戶表一樣,並且具有1:多關係的付款信息表。ElasticSearch:在嵌套數組中搜索字段

這裏是什麼,我試圖做一個簡單的例子:https://gist.github.com/anonymous/6109593

,我希望能找到任何客戶基於paymentInfos的嵌套數組,即在任何比賽找到誰已經有任何用戶paymentInfo與billingZip 10101.此查詢返回沒有結果,我不知道爲什麼。任何人都可以指出我正確的方向,爲什麼這個查詢不起作用,並且如果我可以對我的查詢或映射進行任何更改以使它正確返回用戶?

謝謝!

回答

9

嵌套字段應該使用nested query搜索:

echo "Deleting old ElasticSearch index..." 
curl -XDELETE 'localhost:9200/arrtest' 
echo 
echo "Creating new ElasticSearch index..." 
curl -XPUT 'localhost:9200/arrtest/?pretty=1' -d '{ 
    "mappings" : { 
     "cust2" : { 
     "properties" : { 
      "firstName" : { 
       "type" : "string", 
       "analyzer" : "string_lowercase" 
      }, 
      "lastName" : { 
       "type" : "string", 
       "analyzer" : "string_lowercase" 
      }, 
      "paymentInfos": { 
       "properties": { 
        "billingZip": { 
         "type": "string", 
         "analyzer": "string_lowercase" 
        }, 
        "paypalEmail": { 
         "type": "string", 
         "analyzer": "string_lowercase" 
        } 
       }, 
       "type": "nested" 
      } 
     } 
     } 
    }, 

    "settings" : { 
     "analysis" : { 
     "analyzer" : { 
      "uax_url_email" : { 
       "filter" : [ "standard", "lowercase" ], 
       "tokenizer" : "uax_url_email" 
      }, 

      "string_lowercase": { 
       "tokenizer" : "keyword", 
       "filter" : "lowercase" 
      } 
     } 
     } 
    } 
} 
' 
echo 
echo "Index recreation finished" 

echo "Inserting one record..." 
curl -XPUT 'localhost:9200/arrtest/cust2/1' -d '{ 
    "firstName": "john", 
    "lastName": "smith", 

    "paymentInfos": [{ 
     "billingZip": "10101", 
     "paypalEmail": "[email protected]" 
    }, { 
     "billingZip": "20202", 
     "paypalEmail": "[email protected]" 
    }] 
} 
' 
echo 
echo "Refreshing index to make new records searchable" 
curl -XPOST 'localhost:9200/arrtest/_refresh' 
echo 
echo "Searching for record..." 
curl -XGET 'localhost:9200/arrtest/cust2/_search?pretty=1' -d '{ 
    "sort": [], 
    "query": { 
     "bool": { 
      "should": [], 
      "must_not": [], 
      "must": [{ 
       "nested": { 
        "query": { 
         "query_string": { 
          "fields": ["paymentInfos.billingZip"], 
          "query": "10101" 
         } 
        }, 
        "path": "paymentInfos" 
       } 
      }] 
     } 
    }, 
    "facets": {}, 
    "from": 0, 
    "size": 25 
}' 
echo 
+0

與billingzip 「20202」 爲什麼返回paymentInfos,這正常嗎? –

+0

@ orhanCinar你得到的是原始記錄的來源,其中包括所有嵌套字段。 – imotov

+0

我可以只過濾帶拉鍊10101的孩子,我只需要一個嵌套字段的記錄。這可能嗎 ? –