簡體   English   中英

如何在Elasticsearch中編寫全局查詢?

[英]How to write global query in elasticsearch?

我想使用elasticsearch創建熱門搜索查詢。

我想從brand_name表中匹配category_namebrand_nametitle 匹配項應為詞組以及with或condition。

我的查詢:

$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,
                ],
              ],
            ],
          ],
        ],
      ],
    ],

  ],
];

multi_match使用multi_match ,但如何在此查詢中使用pharse? 我必須寫整個詞來搜索。

例如 :

我想搜索其category_name = Tops的記錄

如果我寫“ tops”或“ top”或“ to”,我想要結果。 但是現在我必須寫出確切的單詞“ Tops”。

提前致謝...

您可以使用match_phrase_prefix ,它與match_phrase相同,只是它允許在文本的最后一項上進行前綴匹配

您需要做的就是在您的multi_match查詢中添加"type": "phrase_prefix" ,如下所示:

"multi_match" => [
  "query" => $searchKey,
  "type": "phrase_prefix",
  "fields" => ["category_name", "brand_name", "title"]
]

讓我知道這是否是您想要的。

理想情況下,您應該將multi_matchcross_fields類型配合使用,它將所有字段視為一個大字段並在其上運行查詢。 但是,由於對這些功能禁用了模糊性,因此僅顯示完全匹配項。

參見Revelant Github問題 https://github.com/elastic/elasticsearch/issues/6866

因此,我的建議是用fuzzy_like_thisfuzzy_like_this 版本1.x )或more_like_thismore_like_this 版本2.x )替換multi_match

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM