2016-06-30 41 views
0

而在elasticsearch搜索galaxy mobiles。 它会创建一个类似于下面的查询,因为galaxy应该在mobiles类别中进行搜索。'必须'和'应该'结合在布尔查询不起作用

{ 
    "query": { 
    "bool": { 
     "should": [{ 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "operator": "and" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase_prefix" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase" 
      } 
     }], 
     "must": [{ 
      "term": { 
       "category": "mobiles" 
      } 
     }] 
    } 
    } 
} 

只有must条件的工作,should条件不工作。 上述查询中是否有问题?

+0

你说的不工作呢?你在期待什么? – femtoRgon

+0

我想要在标题或其他字段中使用银河移动类别的产品。但它只给我手机类别 –

回答

0

如果应该和必须在同一水平上,应该不会有什么影响。所以可能这是你想要的:

{ 
    "query": { 
    "bool": { 
     "must": { 
     "bool": { 
      "should": [{ 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "operator": "and" 
      } 
      }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase_prefix" 
      } 
      }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase" 
      } 
      }] 
     }}, 
     "must": { 
      "term": { 
       "category": "mobiles" 
      } 
     } 
    } 
    } 
} 
0

我得到了如下解决方案。它工作正常。

{ 
    "query": { 
    "filtered": { 
     "query": { 
      "bool": { 
       "should": [{ 
        "multi_match": { 
         "query": "galaxy", 
         "fields": ["title", "product_specs", "category", "brand", "os"], 
         "operator": "and" 
        } 
       }, { 
        "multi_match": { 
         "query": "galaxy", 
         "fields": ["title", "product_specs", "category", "brand", "os"], 
         "type": "phrase_prefix" 
        } 
       }, { 
        "multi_match": { 
         "query": "galaxy", 
         "fields": ["title", "product_specs", "category", "brand", "os"], 
         "type": "phrase" 
        } 
       }] 
      } 
     }, 
     "filter": { 
      "bool": { 
       "must": [{ 
        "term": { 
         "category": "mobiles" 
        } 
       }] 
      } 
     } 
    } 
} 
} 
0

如果你看一下bool弹性搜索文件,你会看到以下内容:

应该

子句(查询)应该会出现匹配的文件内。在 布尔查询中没有must或filter子句,一个或多个应该 子句必须匹配一个文档。可以使用minimum_should_match参数设置匹配 匹配的最小条数。

所以正确的查询是:

{ 
    "query": { 
    "bool": { 
     "should": [{ 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "operator": "and" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase_prefix" 
      } 
     }, { 
      "multi_match": { 
       "query": "galaxy", 
       "fields": ["title", "product_specs", "category", "brand", "os"], 
       "type": "phrase" 
      } 
     }], 
     "minimum_should_match":1, 
     "must": [{ 
      "term": { 
       "category": "mobiles" 
      } 
     }] 
    } 
    } 
} 

请注意,我说的 “minimum_should_match”:1