【问题标题】:Elastic Nest Query returning exact match with Terms弹性嵌套查询返回与条款完全匹配
【发布时间】:2018-09-08 10:23:42
【问题描述】:

我有一个示例机场的索引:

public class FlightIndex
{
    public int Id { get; set; }
    [keyword]
    public string Destination { get; set; }
}

Destination 字段存储“伦敦机场”、“:伦敦机场 (XYZ)”和“伦敦机场 (ABC)”等数据。

我想在Destination上搜索并返回完全匹配。

在下面的查询中,我想要一个目的地与提供的目的地列表匹配的航班列表:

q.Terms(m => m.Field(f => f.Destination).Terms(parameters.Destinations
.Select(_ => _.ToLower()).ToList()));

例如,如果parameters.Destinations 包含“London Airport (ABC)”,则不返回任何内容,但如果它包含“London Airport”,则返回包含“London Airport”的那些。

它似乎不适用于括号。

我不确定它是否需要/可以转义。

【问题讨论】:

    标签: elasticsearch arraylist nest


    【解决方案1】:

    听起来很像Destination 没有被索引为keyword 数据类型;如果是,terms 查询将返回逐字输入值的匹配项。此外,括号不会产生影响,索引值将完全匹配或不匹配。

    我会使用Get Mapping API 检查目标索引中的映射。

    这里有一个例子来展示它的工作原理

    var client = new ElasticClient(settings);
    
    if (client.IndexExists("example").Exists)
    {
        client.DeleteIndex("example");
    }
    
    client.CreateIndex("example", c => c
        .Mappings(m => m
            .Map<FlightIndex>(mm => mm
                .AutoMap()
            )
        )
    );
    
    client.Index(new FlightIndex { Id = 1, Destination = "London Airport (XYZ)" }, i => i
        .Index("example")
        .Refresh(Refresh.WaitFor)
    );
    
    client.Search<FlightIndex>(s => s
        .Index("example")
        .Query(q => q
            .Terms(t => t
                .Field(f => f.Destination)
                .Terms("London Airport (XYZ)")
            )
        )
    );
    

    发送以下请求并接收响应

    HEAD http://localhost:9200/example?pretty=true
    
    Status: 200
    
    ------------------------------
    
    PUT http://localhost:9200/example?pretty=true 
    {
      "mappings": {
        "flightindex": {
          "properties": {
            "id": {
              "type": "integer"
            },
            "destination": {
              "type": "keyword"
            }
          }
        }
      }
    }
    
    Status: 200
    
    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "example"
    }
    
    ------------------------------
    
    PUT http://localhost:9200/example/flightindex/1?pretty=true&refresh=wait_for 
    {
      "id": 1,
      "destination": "London Airport (XYZ)"
    }
    
    Status: 201
    
    {
      "_index" : "example",
      "_type" : "flightindex",
      "_id" : "1",
      "_version" : 1,
      "result" : "created",
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 0,
      "_primary_term" : 1
    }
    
    ------------------------------
    
    POST http://localhost:9200/example/flightindex/_search?pretty=true&typed_keys=true 
    {
      "query": {
        "terms": {
          "destination": [
            "London Airport (XYZ)"
          ]
        }
      }
    }
    
    Status: 200
    
    {
      "took" : 13,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "example",
            "_type" : "flightindex",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "id" : 1,
              "destination" : "London Airport (XYZ)"
            }
          }
        ]
      }
    }
    
    ------------------------------
    

    【讨论】:

    • 您发现目的地没有被索引为关键字。我正在推送文档 client.IndexDocument(new FlightIndex()); .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2016-12-25
    • 2012-07-09
    • 2020-03-25
    相关资源
    最近更新 更多