2017-09-25 112 views
2

我遇到关键字查询问题。ElasticSearch:关键字查询不起作用

我想按键和值过滤categoryProperties。

关键是“颜色”和值包含“马维”

但它给我的文件包含密钥“颜色”和值包含“Beyaz”

你有任何想法,为什么?

REQUEST 我在下面查询searchQuery.categoryProperties.key和searchQuery.categoryProperties.values.value。

{ 
    "query": { 
     "bool": { 
      "must": [{ 
       "nested": { 
        "query": { 
         "bool": { 
          "must": [{ 
           "nested": { 
            "query": { 
             "bool": { 
              "should": [{ 
               "bool": { 
                "must_not": [{ 
                 "term": { 
                  "searchQuery.categoryProperties.key": { 
                   "value": "color" 
                  } 
                 } 
                }] 
               } 
              }, 
              { 
               "bool": { 
                "must": [{ 
                 "term": { 
                  "searchQuery.categoryProperties.key": { 
                   "value": "color" 
                  } 
                 } 
                }, 
                { 
                 "nested": { 
                  "query": { 
                   "term": { 
                    "searchQuery.categoryProperties.values.value": { 
                     "value": "Mavi" 
                    } 
                   } 
                  }, 
                  "path": "searchQuery.categoryProperties.values" 
                 } 
                }] 
               } 
              }] 
             } 
            }, 
            "path": "searchQuery.categoryProperties" 
           } 
          }] 
         } 
        }, 
        "path": "searchQuery" 
       } 
      }] 
     } 
    } 
} 

这里是我的 响应

{ 
    "hits": { 
     "total": 1, 
     "max_score": null, 
     "hits": [{ 
      "_index": "favoritesearchsearchmodelindex_2", 
      "_type": "favoritesearchsearchmodel", 
      "_id": "76175", 
      "_score": null, 
      "_source": { 
       "searchQuery": { 
        "categoryProperties": [ 
        { 
         "key": "color", 
         "values": [{ 
          "value": "Beyaz" 
         }] 
        }] 
       } 
      } 
     }] 
    } 
} 

我的文档的映射: 映射

{ 
    "favoritesearchsearchmodelindex_2": { 
     "mappings": { 
      "favoritesearchsearchmodel": { 
       "properties": { 
        "searchQuery": { 
         "type": "nested", 
         "properties": { 
          "categoryProperties": { 
           "type": "nested", 
           "properties": { 
            "intValue": { 
             "type": "integer" 
            }, 
            "key": { 
             "type": "keyword" 
            }, 
            "values": { 
             "type": "nested", 
             "properties": { 
              "value": { 
               "type": "keyword" 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

回答

1

我解决我的问题。你可以看看真实的答案:Elastic Discuss Forum

根据马克的回应,我改变了我的映射。 新映射

{ 
    "favoritesearchsearchmodelindex_2": { 
     "mappings": { 
      "favoritesearchsearchmodel": { 
       "properties": { 
        "searchQuery": { 
         "type": "nested", 
         "properties": { 
          "categoryProperties": { 
           "properties": { 
            "key": { 
             "type": "keyword" 
            }, 
            "numberValue": { 
             "type": "double" 
            }, 
            "values": { 
             "properties": { 
              "value": { 
               "type": "keyword" 
              } 
             } 
            } 
           } 
          }, 
          "keyList": { 
           "properties": { 
            "value": { 
             "type": "keyword" 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

改变映射后我意识到; 我在寻找searchQuery.categoryProperties.keycolor。我有一个数组,如果其中一个关键是不color其确定为搜索,但不适合我。 我创建了一个keyList数组,并将所有分组密钥searchQuery.categoryProperties.key设置为keyList对象。 现在我在寻找第一个对keyList。它给了我正确的回应。这解决了我的问题。

这里是正确的请求

{ 
    "query": { 
     "bool": { 
      "must": [{ 
       "nested": { 
        "query": { 
         "bool": { 
          "filter": [{ 
           "bool": { 
            "should": [{ 
             "bool": { 
              "must_not": [{ 
               "term": { 
                "searchQuery.keyList.value": { 
                 "value": "color" 
                } 
               } 
              }] 
             } 
            }, 
            { 
             "bool": { 
              "must": [{ 
               "term": { 
                "searchQuery.categoryProperties.key": { 
                 "value": "color" 
                } 
               } 
              }, 
              { 
               "term": { 
                "searchQuery.categoryProperties.values.value": { 
                 "value": "Mavi" 
                } 
               } 
              }] 
             } 
            }] 
           } 
          }] 
         } 
        }, 
        "path": "searchQuery" 
       } 
      }] 
     } 
    } 
}