使用ID列表过滤的Elasticsearch多重匹配

问题描述 投票:0回答:1

我对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

编辑:

我更改了索引,并从我的SupplierId字段中删除了Index(false)

这是更新后在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# elasticsearch nest
1个回答
0
投票

在映射中,您将SupplierId指定为excluded from the index,因此将无法对其进行搜索。

.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())))));

足够。

希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.