如何在弹性搜索中编写全局查询? [英] How to write global query in elasticsearch?

查看:127
本文介绍了如何在弹性搜索中编写全局查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要匹配 category_name brand_name title from elasticsearch table。比赛应该是短语和条件。



我的查询:

  $ query = [
bool=> [
must=> [
[match=> [is_published=> 1]],
[
multi_match=> [
query=> $ searchKey,
fields=> [category_name,brand_name,title]
]
],
[
'嵌套'=> [
'path'=> 'category_with_in_title.parent_cat',
'query'=> [
'bool'=> [
'must'=> [

['match'=> [
'category_with_in_title.parent_cat.status'=> 1,
]],
],
],
],
],
],
[
' '=> [
'path'=> 'brand',
'query'=> [
'bool'=> [
'must'=> [
'match'=> [
'brand.approved_status'=> 1,
],
],
],
],
],
],
[
' => [
'path'=> 'default_product_low_price_with_seller.seller_detail',
'query'=> [
'bool'=> [
'must'=> [
'match'=> [
'default_product_low_price_with_seller.seller_detail.approve_status'=> 1,
],
],
],
],
],

],
[
'nested'=> [
'path'=> 'default_product_low_price_with_seller',
'query'=> [
'bool'=> [
'must'=> [
'match'=> [
'default_product_low_price_with_seller.status'=> 1,
],
],
],
],
],
],
],

],
];

我使用 multi_match ,但如何在这个查询中使用pharse?我必须写整个字来搜索。



例如:



我想搜索 category_name = Tops



如果我写顶或顶或到,我想要结果。但是现在我必须写Tops的确切单词。



提前感谢...

解决方案

理想情况下,您应该使用 multi_match cross_fields 类型,将所有字段视为一个大字段,并在其上运行查询。然而,由于这些模糊性被禁用,只有精确的匹配将显示。



请参阅Revelant Github问题 https://github.com/elastic/elasticsearch/issues/6866


$ b $因此,我的建议是将 multi_match 替换为 fuzzy_like_this elasticsearch version 1.x )或 more_like_this ()。

  $ query = [
bool=> [
must=> [
[match=> [is_published=> 1]],
[
fuzzy_like_this=> [
fields=> [category_name,brand_name,title],
like_text=> $ searchKey
]
],
....


I want to create top search query using elasticsearch.

I want to match category_name, brand_name and title from elasticsearch table. The match should be phrase and with or condition.

My query:

$query = [
  "bool" => [
    "must" => [
      ["match" => ["is_published" => 1]],
      [
        "multi_match" => [
          "query" => $searchKey,
          "fields" => ["category_name", "brand_name", "title"]
        ]
      ],
      [
        'nested' => [
          'path' => 'category_with_in_title.parent_cat',
          'query' => [
            'bool' => [
              'must' => [

                ['match' => [
                  'category_with_in_title.parent_cat.status' => 1,
                ]],
              ],
            ],
          ],
        ],
      ],
      [
        'nested' => [
          'path' => 'brand',
          'query' => [
            'bool' => [
              'must' => [
                'match' => [
                  'brand.approved_status' => 1,
                ],
              ],
            ],
          ],
        ],
      ],
      [
        'nested' => [
          'path' => 'default_product_low_price_with_seller.seller_detail',
          'query' => [
            'bool' => [
              'must' => [
                'match' => [
                  'default_product_low_price_with_seller.seller_detail.approve_status' => 1,
                ],
              ],
            ],
          ],
        ],

      ],
      [
        'nested' => [
          'path' => 'default_product_low_price_with_seller',
          'query' => [
            'bool' => [
              'must' => [
                'match' => [
                  'default_product_low_price_with_seller.status' => 1,
                ],
              ],
            ],
          ],
        ],
      ],
    ],

  ],
];

I use multi_match for that but how to use pharse in this query? I have to write whole word to search.

For example :

I want to search the record whose category_name = Tops

I want result if i write "tops" or "top" or "to". But right now I have to write "Tops" the exact word.

Thanks in advance...

解决方案

Ideally you should use multi_match with cross_fields type, which will treat all fields as ONE big field and run your query on it. However, due to fuzziness being disabled on these, only the exact match will show.

See Revelant Github Issue https://github.com/elastic/elasticsearch/issues/6866

Hence, my recommendation is to replace multi_match with fuzzy_like_this (elasticsearch version 1.x) or more_like_this (elasticsearch version 2.x).

$query = [
  "bool" => [
    "must" => [
      ["match" => ["is_published" => 1]],
      [
        "fuzzy_like_this" => [
            "fields" => ["category_name", "brand_name", "title"],
            "like_text" => $searchKey
        ]
      ],
      ....

这篇关于如何在弹性搜索中编写全局查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆