【问题标题】:How to write global query in elasticsearch?如何在elasticsearch中编写全局查询?
【发布时间】:2016-08-02 20:25:46
【问题描述】:

我想使用 elasticsearch 创建热门搜索查询。

我想从 elasticsearch 表中匹配 category_namebrand_nametitle。匹配应该是短语和 with or 条件。

我的查询:

$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,但如何在此查询中使用短语?我必须写整个单词才能搜索。

例如:

我要搜索category_name = Tops的记录

如果我写“tops”或“top”或“to”,我想要结果。但是现在我必须准确地写出“Tops”。

提前谢谢...

【问题讨论】:

    标签: php search elasticsearch


    【解决方案1】:

    您可以使用match_phrase_prefix,它与match_phrase 相同,除了它允许在文本中的最后一个词进行前缀匹配

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

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

    如果这就是你要找的,请告诉我。

    【讨论】:

    【解决方案2】:

    理想情况下,您应该使用multi_matchcross_fields 类型,这会将所有字段视为一个大字段并在其上运行您的查询。但是,由于在这些上禁用了模糊性,因此只会显示完全匹配的内容。

    查看相关的 Github 问题 https://github.com/elastic/elasticsearch/issues/6866

    因此,我的建议是将multi_match 替换为fuzzy_like_this (elasticsearch version 1.x) 或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
            ]
          ],
          ....
    

    【讨论】:

    • 谢谢先生,它有效,但我必须写完整的单词才能搜索。例如,我想获取 category_name = Tops 的记录,然后我必须写 'tops' 或 'top' 来获取该记录。如果我写“to”(搜索顶部),我不会得到任何记录。
    • fuzzy_like_this数组中,添加"fuzziness" => 3。这将增加匹配的可能性。这也可能表明您的索引可能不会被分析。见:elastic.co/guide/en/elasticsearch/guide/current/…
    • 先生,我可以使用查询而不改变分析的结构吗?
    • 我想从句子中搜索部分搜索但不改变结构
    • 如果您切换映射,则必须从头开始重建索引。
    猜你喜欢
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    相关资源
    最近更新 更多