为什么我的术语查询不起作用?

时间:2018-03-21 19:49:28

标签: elasticsearch

这是我的设置文件 -

{
    "settings": {
        "mappings": {
            "default": {
                "properties": {
                    "AIRPORT_CODE": {
                        "type": "text"
                    },
                    "PROVINCE_NAME": {
                        "type": "text"
                    },
                    "AIRPORT_NAME": {
                        "type": "text"
                    },
                    "CITY_NAME": {
                        "type": "text"
                    },
                    "TYPE": {
                        "type": "keyword"
                    },
                    "COUNTRY_NAME": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

以下是我摄入索引的2个文件 -

文档 - 1

{
  "AIRPORT_CODE": "SQA",
  "PROVINCE_NAME": "California",
  "AIRPORT_NAME": "Santa Ynez Airport",
  "CITY_NAME": "SANTA YNEZ",
  "TYPE": "AIRPORT"
}

文件 - 2

{
  "PROVINCE_NAME": "SANTIAGO",
  "CITY_NAME": "SANTIAGO",
  "COUNTRY_NAME": "DOMINICAN REPUBLIC",
  "TYPE": "HOTEL"
}

这是我的搜索查询 -

{
    "size": 4,
    "timeout": "2m",
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "SQA"
                    }
                }
            ],
            "filter": [
                {
                    "term": {
                        "TYPE": "AIRPORT"
                    }
                }
            ]
        }
    },
    "explain": false
}

“TYPE”字段已被定义为关键字。但上面的查询不会返回任何内容。如果将“TYPE”值从“AIRPORT”更改为“airport”(小写),我会得到结果。我做错了什么?

注意#我的努力是让这个查询工作,所以我可以利用过滤器缓存(节点查询缓存)。

提前致谢!

更新 - 1:添加GET索引/ _mapping

{
  "rc-filter-cache": {
    "mappings": {
      "default": {
        "properties": {
          "AIRPORT_CODE": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "AIRPORT_NAME": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "CITY_NAME": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "COUNTRY_NAME": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "PROVINCE_NAME": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "TYPE": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "settings": {
            "properties": {
              "index": {
                "properties": {
                  "analysis": {
                    "properties": {
                      "analyzer": {
                        "properties": {
                          "keylower": {
                            "properties": {
                              "tokenizer": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              },
              "mappings": {
                "properties": {
                  "default": {
                    "properties": {
                      "properties": {
                        "properties": {
                          "AIRPORT_CODE": {
                            "properties": {
                              "type": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              }
                            }
                          },
                          "AIRPORT_NAME": {
                            "properties": {
                              "type": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              }
                            }
                          },
                          "CITY_NAME": {
                            "properties": {
                              "type": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              }
                            }
                          },
                          "COUNTRY_NAME": {
                            "properties": {
                              "type": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              }
                            }
                          },
                          "PROVINCE_NAME": {
                            "properties": {
                              "type": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              }
                            }
                          },
                          "TYPE": {
                            "properties": {
                              "analyzer": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              },
                              "type": {
                                "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

以下是解决这个问题的方法:

DELETE index
PUT index
{
  "mappings": {
    "default": {
      "properties": {
        "AIRPORT_CODE": {
          "type": "text"
        },
        "PROVINCE_NAME": {
          "type": "text"
        },
        "AIRPORT_NAME": {
          "type": "text"
        },
        "CITY_NAME": {
          "type": "text"
        },
        "TYPE": {
          "type": "keyword"
        },
        "COUNTRY_NAME": {
          "type": "text"
        }
      }
    }
  }
}
PUT index/default/1
{
  "AIRPORT_CODE": "SQA",
  "PROVINCE_NAME": "California",
  "AIRPORT_NAME": "Santa Ynez Airport",
  "CITY_NAME": "SANTA YNEZ",
  "TYPE": "AIRPORT"
}
PUT index/default/2
{
  "PROVINCE_NAME": "SANTIAGO",
  "CITY_NAME": "SANTIAGO",
  "COUNTRY_NAME": "DOMINICAN REPUBLIC",
  "TYPE": "HOTEL"
}
GET index/_search
{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "TYPE": "AIRPORT"
                    }
                }
            ]
        }
    }
}