首页 > 解决方案 > 使用 ID 列表过滤的 Elasticsearch 多重匹配

问题描述

我对 ES 比较陌生,我正在尝试解决以下问题。

我使用以下配置在我的 Elasticsearch 中创建了一个索引:

client.Indices.Create(lineItemIndex,
                c => c
                    .Settings(s => s
                        .Setting("max_ngram_diff", 13)
                        .Analysis(a => a
                            .Tokenizers(tf => tf
                                .NGram("mynGram", td => td
                                    .MaxGram(15).MinGram(2)))
                            .Analyzers(aa => aa
                                .Custom("mynGram_analyzer", ca => ca
                                    .Filters(new List<string> {"lowercase"})
                                    .Tokenizer("mynGram")))))
                    .Map<ElasticSearchLineItem>(m => m
                        .Properties(ps => ps
                            .Text(ss => ss
                                .Name(na => na.LineItemName)
                                .Fields(ff => ff
                                    .Keyword(k => k
                                        .Name("keyword"))
                                    .Text(tx => tx
                                        .Name("fulltext")
                                        .Analyzer("whitespace")
                                        .Boost(10.0))
                                    .Text(tx => tx
                                        .Name("partial")
                                        .Analyzer("mynGram_analyzer")
                                        .Boost(1.0)))))
                        .Properties(ps => ps
                            .Keyword(kw => kw
                                .Name(na => na.LineItemId)
                                .Index(false)))
                        .Properties(ps => ps
                            .Keyword(kw => kw
                                .Name(na => na.Id)
                                .Index(false)))
                        .Properties(ps => ps
                            .Text(ss => ss
                                .Name(na => na.LineItemNumber)
                                .Fields(ff => ff
                                    .Keyword(k => k
                                        .Name("keyword"))
                                    .Text(tx => tx
                                        .Name("fulltext")
                                        .Analyzer("whitespace")
                                        .Boost(10.0))
                                    .Text(tx => tx
                                        .Name("partial")
                                        .Analyzer("mynGram_analyzer")
                                        .Boost(1.0)))))
                        .Properties(ps => ps
                            .Keyword(ss => ss
                                .Name(na => na.SupplierName)
                                .Index(false)))
                        .Properties(ps => ps
                            .Keyword(ss => ss
                                .Name(na => na.Unit)
                                .Index(false)))
                        .Properties(ps => ps
                            .Number(ss => ss
                                .Name(na => na.PriceAmount)
                                .Type(NumberType.ScaledFloat).ScalingFactor(100)
                                .Index(false)))
                        .Properties(ps => ps
                            .Keyword(ss => ss
                                .Name(na => na.Currency)
                                .Index(false)))
                        .Properties(ps => ps
                            .Keyword(ss => ss
                                .Name(na => na.SupplierId)
                                .Index(false)))
                        .Properties(ps => ps
                            .Text(ss => ss
                                .Name(na => na.ImageUrl)
                                .Index(false)))
                        .Properties(ps => ps
                            .Text(ss => ss
                                .Name(na => na.SupplierPriceListId)
                                .Index(false)))));

我的解决方案是我们有一个搜索框,用于搜索。

但是,我们还假设能够基于 过滤搜索SupplierId。因此,进行搜索的人可能有多个SupplierId他们只想查看结果的人。

我试图创建以下查询:

var esSearch2 = new SearchDescriptor<ElasticSearchLineItem>()
            .From(0)
            .Take(250)
            .Query(q => q
                .Bool(b => b
                    .Must(mu => mu
                        .MultiMatch(m => m
                            .Fields(f => f
                                .Field(ff => ff
                                    .LineItemName.Suffix("fulltext"))
                                .Field(ff => ff
                                    .LineItemName.Suffix("partial"))
                                .Field(ff => ff
                                    .LineItemNumber.Suffix("fulltext"))
                                .Field(ff => ff
                                    .LineItemNumber.Suffix("partial")))
                        .Query(request.SearchWord)
                        .Fuzziness(Fuzziness.Auto)
                        ))                        
                    .Filter(f => f
                        .Terms(t => t
                            .Verbatim()
                            .Field(p => p
                                .SupplierId.Suffix("keyword"))
                            .Terms(request.ListOfFavorites.ToArray())))));

无论是否request.ListOfFavorites为空,这都不返回任何内容。但是如果我删除我的过滤器,它将正确返回结果。

我想我错过了一些东西,或者我的订单搞砸了。任何人都可以在这里帮助吗?

注意:我使用的是 ES 7.5.1 和 NEST 7.5.1

编辑:

我对我的索引进行了更改,并Index(false)从我的供应商 ID 字段中删除。

这是更新后在kibana中显示的映射

{
"mapping": {
"properties": {
  "currency": {
    "type": "keyword",
    "index": false
  },
  "id": {
    "type": "keyword",
    "index": false
  },
  "imageUrl": {
    "type": "text",
    "index": false
  },
  "lineItemId": {
    "type": "keyword",
    "index": false
  },
  "lineItemName": {
    "type": "text",
    "fields": {
      "fulltext": {
        "type": "text",
        "boost": 10,
        "analyzer": "whitespace"
      },
      "keyword": {
        "type": "keyword"
      },
      "partial": {
        "type": "text",
        "analyzer": "mynGram_analyzer"
      }
    }
  },
  "lineItemNumber": {
    "type": "text",
    "fields": {
      "fulltext": {
        "type": "text",
        "boost": 10,
        "analyzer": "whitespace"
      },
      "keyword": {
        "type": "keyword"
      },
      "partial": {
        "type": "text",
        "analyzer": "mynGram_analyzer"
      }
    }
  },
  "priceAmount": {
    "type": "scaled_float",
    "index": false,
    "scaling_factor": 100
  },
  "supplierId": {
    "type": "keyword"
  },
  "supplierName": {
    "type": "keyword",
    "index": false
  },
  "supplierPriceListId": {
    "type": "text",
    "index": false
  },
  "unit": {
    "type": "keyword",
    "index": false
  }
}
}
}

标签: c#elasticsearchnest

解决方案


在您的映射中,您指定要从索引SupplierId排除,因此您将无法对其进行搜索。

.Properties(ps => ps
    .Keyword(ss => ss
        .Name(na => na.SupplierId)
        .Index(false)))

此外,您不需要为您的字段指定后缀,因为它没有定义为多字段,所以简单

.Filter(f => f
    .Terms(t => t
        .Verbatim()
        .Field(p => p.SupplierId)
        .Terms(request.ListOfFavorites.ToArray())))));

足够的。

希望有帮助。


推荐阅读