2017-01-29 14 views
0

嵌套查詢我有嵌套字段該指數映射:如何使用我疊的搜索條件和在ElasticSearch

"customerpropertieses": { 
    "_parent": { 
     "type": "customerprofile" 
    }, 
    "_routing": { 
     "required": true 
    }, 
    "properties": { 
     "id": { 
     "type": "string" 
     }, 
     "parentId": { 
     "type": "string" 
     }, 
     "properties": { 
     "type": "nested", 
     "properties": { 
      "extentionPropertyId": { 
      "type": "long" 
      }, 
      "propertyName": { 
      "type": "string" 
      }, 
      "value": { 
      "type": "string" 
      } 
     } 
     } 
    } 
    } 

我想找到customerpropertieses其中propertyName=criteria1 & value=value1 & propertyName=criteria2 & value=value2

查詢我生成這是:

{ 
    "from": 0, 
    "size": 10, 
    "sort": [ 
    { 
     "_score": { 
     "order": "desc" 
     } 
    } 
    ], 
    "query": { 
    "nested": { 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "match": { 
       "properties.propertyName": { 
        "query": "criteria1 " 
       } 
       } 
      }, 
      { 
       "match": { 
       "properties.value": { 
        "boost": 10.0, 
        "query": "value1" 
       } 
       } 
      }, 
      { 
       "match": { 
       "properties.propertyName": { 
        "query": "criteria2" 
       } 
       } 
      }, 
      { 
       "match": { 
       "properties.value": { 
        "boost": 10.0, 
        "query": "value2" 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "path": "properties", 
     "inner_hits": { 
     "explain": false 
     }, 
     "_name": "nested_properties" 
    } 
    } 
} 

我得到0個結果回來,我肯定有這些特徵的數據。當我看只有propertyName=criteria1 & value=value1或參數propertyName = criteria2 &值=數值'

的問題是我怎麼可以疊加在嵌套查詢使用&搜索條件搜索工作正常?

回答

1

請嘗試以下查詢。從你的查詢的角度來看,我猜想你想匹配的文件有兩個嵌套文檔,分別是value1,criteria1和value2,criteria2分別是value和criteria字段。

{ 
    "from": 0, 
    "size": 10, 
    "sort": [{ 
     "_score": { 
      "order": "desc" 
     } 
    }], 
    "query": { 
     "bool": { 
      "must": [{ 
       "nested": { 
        "query": { 
         "bool": { 
          "must": [{ 
           "match": { 
            "properties.propertyName": { 
             "query": "criteria2" 
            } 
           } 
          }, { 
           "match": { 
            "properties.value": { 
             "boost": 10.0, 
             "query": "value2" 
            } 
           } 
          }] 
         } 
        }, 
        "path": "properties", 
        "inner_hits": { 
         "explain": false 
        }, 
        "_name": "nested_properties" 
       } 
      }, { 
       "nested": { 
        "query": { 
         "bool": { 
          "must": [{ 
           "match": { 
            "properties.propertyName": { 
             "query": "criteria1 " 
            } 
           } 
          }, { 
           "match": { 
            "properties.value": { 
             "boost": 10.0, 
             "query": "value1" 
            } 
           } 
          }] 
         } 
        }, 
        "path": "properties", 
        "inner_hits": { 
         "explain": false 
        }, 
        "_name": "nested_properties" 
       } 
      }] 
     } 

    } 
} 

我收錄了以下文檔製作猜測,修改查詢 以下內容將符合查詢條件上述

{ 
     "parentId" : "3434", 
     "properties" : [{ 
     "extentionPropertyId" : 24, 
     "propertyName" : "criteria1", 
     "value" : "value1" 
     },{ 
     "extentionPropertyId" : 24, 
     "propertyName" : "criteria2", 
     "value" : "value2" 
     }] 
    } 

希望這有助於。 謝謝

+0

umm或許我沒有正確解釋它customerpropertieses.properties是我的C#代碼中的List [Tuple [propertyName,value]]。我希望查詢匹配此列表中的多個項目,即給我所有的customerpropertieses,其中propertyName = tomato和value = red AND propertyName = corn和value = yellow。現在這將返回在列表中有兩個屬性的customerpropertieses:紅番茄和黃色玉米 – Adrian

+0

是的,這正是這個查詢正在做criteria2 value2和criteria1 value1。我添加了兩個嵌套的必須過濾器。 – user3775217

+0

哦,我明白了;所以如果我想匹配4個標準,我需要4個嵌套查詢。正確? – Adrian

相關問題