简体   繁体   English

在ElasticSearch上使用NEST - 如何使用部分填充的对象作为搜索条件

[英]Using NEST on ElasticSearch - How do i use a partially populated object as a search criteria

So I have successfully created my index of "Package" objects and a straight forward text query is working perfectly. 所以我已经成功创建了我的“Package”对象索引,并且直接的文本查询工作正常。

I'd love to know if / how i can use a partially populated object (of type "Package") as the criteria for my search? 我想知道是否/如何使用部分填充的对象(类型为“Package”)作为我的搜索条件?

Packages looks something like: 包看起来像:

var packages =  new List<Package> {
            new Package {
                Name = "Maverick",
                TargetBusiness = new Business {
                    Industry = "Retail",
                    BusinessType = BusinessType.Product,
                    LocationType = LocationType.Store
                },
                Description = "Standard package for retail shops"
            },
            new Package {
                Name = "Goose",
                TargetBusiness = new Business {
                    Industry = "Retail",
                    BusinessType = BusinessType.Product,
                    LocationType = LocationType.Online
                },
                Description = "Standard package for ecommerce shops"
            },
            new Package {
                Name = "Viper",
                TargetBusiness = new Business {
                    Industry = "Advertising",
                    BusinessType = BusinessType.Service,
                    LocationType = LocationType.Office
                },
                Description = "Standard package test retail"
            }
        }

query currently looks something like: 查询当前看起来像:

var result = client.Search<Package>(x => x.Query(q => q.QueryString(qs => qs.Query("q=retail"))));

But id like to have something like: 但是我喜欢这样的东西:

var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));

I hope I'm making sense :D Thanks in advance 我希望我有意义:D先谢谢你

var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));

Can never work because Nest cannot infer what type of query to use for each property on your object, (ie term, prefix, wildcard, query_string, etc etc etc). 永远不会工作,因为Nest无法推断出对象上的每个属性使用什么类型的查询(即术语,前缀,通配符,query_string等等)。

In your example q=retail only works because elasticsearch will break the query up into q OR retail . 在您的示例中, q=retail仅适用,因为elasticsearch会将查询分解为q OR retail You can target fields using regular lucene syntax ie targetBusiness.industry:retail . 您可以使用常规的lucene语法(即targetBusiness.industry:retail来定位字段。

In elasticsearch if your querystring is not bound to a field it will by default search in the _all field which will have all the terms for all the properties of an object. 在elasticsearch中,如果您的查询字符串未绑定到字段,它将默认搜索_all字段,该字段将包含对象的所有属性的所有项。 Which is why if you really have a lot of data turning off _all support is usually a really good idea. 这就是为什么如果你真的关闭了很多数据_all支持通常是个好主意。

Nest doesn't currently have such a functionality where it can take a partially filled object and translate it into an elasticsearch query_string query. Nest目前没有这样的功能,它可以采用部分填充的对象并将其转换为elasticsearch query_string查询。

Say if this is your query: 如果这是您的查询,请说:

client.Search<Package>(s=>s
    .From(0)
    .Size(10)
    .Filter(f=>
        f.Term(p=>p.TargetBusiness.Industry, "Advertising")
        && f.Exists(p=>p.Name)
    )
    .Query(q=>
       q.QueryString(qs=>qs
           .Query("ecommerce")
           .Operator(Operator.and)
       )
    )
)

In the above example you'll have to create your own method that gives all the terms to search for in the query string query based on your package. 在上面的示例中,您将必须创建自己的方法,根据您的包提供在查询字符串查询中搜索的所有术语。

ie

public string MyPackageQueryString(Package package) 
{
    var myTerms = List<string>();
    myTerms.Add(package.Name);
    if (package.TargetBusiness != null)
    {
        myTerms.Add(package.Industry)
        ....
    }
    ...
    return string.Join(" ", myTerms.Where(t=>!string.IsNullOrWhiteSpace(t)));
}

and then 接着

client.Search<Package>(s=>s
    .From(0)
    .Size(10)
    .Filter(f=>
        f.Term(p=>p.TargetBusiness.Industry, "Advertising")
        && f.Exists(p=>p.Name)
    )
    .Query(q=>
       q.QueryString(qs=>qs
           .Query(this.MyPackageQueryString(package))
           .Operator(Operator.or)
       )
    )
)

That really helped put me on the right track - in the end that looked like a sort of cross search: Searching every field with every other field which i didn't want. 这确实帮助我走上了正确的轨道 - 最终看起来像是一种交叉搜索:用我不想要的其他所有领域搜索每个领域。

Ended up (possible incorrectly) going with: 结束(可能不正确)继续:

return _searchClient.Search<Package>(s => s.Query(q => 
        q.Term("industry", criteriaPackage.TargetBusiness.Industry.ToLower()) ||
        q.Term("description", criteriaPackage.TargetBusiness.Description.ToLower()) ||
        q.Term("businessType",((int)criteriaPackage.TargetBusiness.BusinessType).ToString()) ||
        q.Term("locationType", ((int)criteriaPackage.TargetBusiness.LocationType).ToString()) ||
        q.Term("marketSegment", criteriaPackage.TargetBusiness.MarketSegment.ToLower()) ||
        q.Term("offer", criteriaPackage.TargetBusiness.Offer.ToLower()))
      ).Documents;

Which after a few unit tests seemed to be producing the results i was wanting. 经过几次单元测试似乎产生了我想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Elasticsearch NEST使用匹配搜索返回结果? - How do I use Elasticsearch NEST to return results using match search? 我如何使用Nest API 5在Elasticsearch中进行重新索引以从Elastic Search 1.4迁移到ElasticSearch 5 - How can i do reindexing in elasticsearch to migrate from elastic search 1.4 to elasticsearch 5 using Nest api 5 如何使用NEST构造来自多个类型的字段且没有魔术字符串的ElasticSearch搜索 - How do I construct an ElasticSearch search using NEST with fields from multiple types without magic strings 有没有办法搜索列表 <T> 使用部分填充的对象? - Is there a way to search a List<T> using a partially populated object? 如何使用NEST在ElasticSearch嵌套对象中搜索 - How to search in ElasticSearch nested objects using NEST 如何使用NEST为Elasticsearch指定索引? - How do I specify an index for Elasticsearch using NEST? 如何使用NEST准确表示此ElasticSearch查询? - How do I accurately represent this ElasticSearch query using NEST? 使用Nest(Elasticsearch)进行方面搜索 - Facet search using Nest (Elasticsearch) Nest-ElasticSearch.Net-如何分割搜索属性返回的FacetFilter命中计数 - Nest - ElasticSearch.Net - How do I split the FacetFilter hit count returned by a search property 对 elasticsearch 使用 NEST。 如何根据孩子属性搜索父母 - Using NEST for elasticsearch. How can I search for parents based on children attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM