简体   繁体   English

NEST C#Elasticsearch查询,其中属性是一个列表

[英]NEST C# Elasticsearch query where property is a list

I'm trying to query the following entity with NEST (.NET elasticsearch wrapper): 我正在尝试使用NEST(.NET elasticsearch包装器)查询以下实体:

public class TourIndex
{

    public Guid ProductId { get; set; }

    public Guid[] Periodes { get; set; }

    public Guid[] Countries { get; set; }

    ...
}

I want to get all TourIndex object where one of the Countries is guid x. 我想获取所有其中一个国家为guid x的TourIndex对象。

My index is correct, I can see my TourIndex objects when surfing to http://localhost:9200/my-index/_search?pretty=true&q=*:* 我的索引是正确的,浏览到http://localhost:9200/my-index/_search?pretty=true&q=*:*时,我可以看到我的TourIndex对象

And my query attempt is: 我的查询尝试是:

System.Func<QueryDescriptor<TourIndex>, QueryContainer> elasticSearchQuery = (q => q.Term(x => x.Countries.First(), myGuidX));

var client = ElasticSearchHelper.CreateNestClient();
var searchResponse = client.Search<TourIndex>(s => s.Query(elasticSearchQuery));

return searchResponse.Documents;

Unfortunately I get no results. 不幸的是我没有结果。 The respons is Valid but no documents are returned. 响应有效,但不返回任何文档。 I've doublechecked there is a valid TourIndex in my index that has my country guid. 我已经仔细检查了索引中是否有有效的TourIndex,该索引具有我的国家/地区向导。

An example of an indexed object: 索引对象的示例:

{
      "_index" : "my-index",
      "_type" : "tour",
      "_id" : "WRAG141004",
      "_score" : 1.0,
      "_source":{
  "erpTourId": "SNG141004",
  "productId": "9d28694e-d705-e411-93f9-00155d01210a",
  "countries": [
    "01cad5a4-caf4-4d01-88e7-936c0827b13e"
  ],
  "productType": "88e436ff-d605-e411-93f9-00155d01210a",
  "subProductType": "ece1fa1b-d705-e411-93f9-00155d01210a",
  "dayCount": 8,
  "price": 7852,
  "groupCount": 0,
  "isPromotion": true,
  "composition": []
}

So myGuidX could be "01cad5a4-caf4-4d01-88e7-936c0827b13e" for example. 例如,myGuidX可以是"01cad5a4-caf4-4d01-88e7-936c0827b13e"

The analyser/mapping: 分析器/映射:

{
    "live-to-travel-index": {
        "mappings": {
            "tour": {
                "properties": {
                    "available": {
                        "type": "long"
                    },
                    "booked": {
                        "type": "long"
                    },
                    "capacity": {
                        "type": "long"
                    },
                    "composition": {
                        "type": "string"
                    },
                    "countries": {
                        "type": "string"
                    },
                    "dayCount": {
                        "type": "long"
                    },
                    "globusTourId": {
                        "type": "string"
                    },
                    "groupCount": {
                        "type": "long"
                    },
                    "isPromotion": {
                        "type": "boolean"
                    },
                    "options": {
                        "type": "long"
                    },
                    "periodes": {
                        "type": "string"
                    },
                    "price": {
                        "type": "long"
                    },
                    "productId": {
                        "type": "string"
                    },
                    "productType": {
                        "type": "string"
                    },
                    "queue": {
                        "type": "long"
                    },
                    "subProductType": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

I didn't configure anything in c#, this is all default I think. 我没有在C#中配置任何东西,我认为这是默认设置。

Update 更新资料

I've found a solution to the problem, however I think it's a bad practice. 我已经找到了解决问题的办法,但是我认为这是一个不好的做法。

This code gives me results: 这段代码给了我结果:

elasticSearchQuery = (q => q.Match(m => { m.Query("*" +    query.CountryId.Value.ToString() + "*"); }));
var client = ElasticSearchHelper.CreateNestClient();
var searchResponse = client.Search<TourIndex>(s => s.Query(elasticSearchQuery));

My guess is that the wildcards will decrease performance. 我的猜测是通配符会降低性能。

Can you provide some details on the guid format, your field mappings and analyzers? 您能否提供有关guid格式,字段映射和分析器的一些详细信息? It's possible that the values are not being searched for/analyzed as you would expect. 可能未如您期望的那样搜索/分析这些值。 Also, do you have Kibana/Kopf installed? 另外,您是否已安装Kibana / Kopf?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM