【问题标题】:How do I search on a property using Elastic Search for Node.js?如何使用 Elastic Search for Node.js 搜索属性?
【发布时间】:2019-10-07 19:17:28
【问题描述】:

我正在将各种文档摄取到 Elastic Search,如下所示:

await client.index({
  id: url,
  index: 'docs',
  body: {
    'Url': url,
    'Accessed': new Date().toISOString(),
    'Content': content,
    'Title': title,
  }
});

现在,我只想对_source.Title 进行搜索。这应该是对英文文本的模糊搜索。

如何使用 Elastic Search 库实现这一目标?

  const result = await client.search({
    index: 'docs',
    from: 0,
    size: 20,
    body: {
      "_source": [
        "Url",
        "Title",
        "Accessed",
      ],
      query: {
        // What goes here?
      }
    },
  });

我正在使用"@elastic/elasticsearch": "^7.4.0"

【问题讨论】:

    标签: node.js elasticsearch


    【解决方案1】:

    有很多方法可以解决这个问题。但最初我会使用带有模糊性的match 查询:

      const result = await client.search({
        index: 'docs',
        from: 0,
        size: 20,
        body: {
          "_source": [
            "Url",
            "Title",
            "Accessed",
          ],
          query: {
            "match" : {
              "title" : {
                "query" : "some title terms",
                "fuzziness": 2
              }
            }
          }
        },
      });
    

    【讨论】:

      【解决方案2】:

      https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html

        const result = await client.search({
          index: 'docs',
          from: 0,
          size: 20,
          body: {
            "_source": [
              "Url",
              "Title",
              "Accessed",
            ],
            query: {
               "fuzzy": {
                  "title": {
                     "value": "{searchQuery}"
                  }
               }
            }
          },
        });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 2020-08-21
        相关资源
        最近更新 更多