ElasticSearch查询忽略其他搜索词

时间:2018-05-01 08:59:43

标签: elasticsearch search lucene full-text-search

我正在使用ElasticSearch在多个结构化字段上构建一个自由形式搜索即用型功能。人们搜索的主要字段是first_namelast_namecity

问题:以下两次搜索David SalazarDavid Salazar Denver返回的结果与“Denver”似乎被忽略的结果相同。

我认为这是我的查询的问题,但我仍然坚持如何改变它以获得我正在寻找的东西。

以下是查询:

GET index_name/_search
{
  "query": {
    "multi_match": {
      "fields": [
        "first_name","middle_name", "last_name", "city", "county", "street"],
      "query": "David Salazar Denver",
      "type": "cross_fields",
      "use_dis_max": false
    }
  },
  "size": 10
}

以下是索引设置和字段映射的相关部分

{
  "index": {
    "aliases": {},
    "mappings": {
      "type": {
        "properties": {
          "city": {
            "type": "keyword"
          },
          "county": {
            "type": "keyword"
          },
          "first_name": {
            "type": "text",
            "analyzer": "synonym_autocomplete",
            "search_analyzer": "standard"
          },
          "last_name": {
            "type": "text",
            "analyzer": "autocomplete",
            "search_analyzer": "standard"
          },
          "middle_name": {
            "type": "text",
            "analyzer": "synonym_autocomplete",
            "search_analyzer": "standard"
          },
          "street": {
            "type": "text",
            "analyzer": "autocomplete",
            "search_analyzer": "standard"
          },
        }
      }
    },
    "settings": {
      "index": {
        [...]
        "analysis": {
          "filter": {
            "synonym": {
              "type": "synonym",
              "synonyms": [Long list of nicknames]
            },
            "autocomplete_filter": {
              "type": "edge_ngram",
              "min_gram": "2",
              "max_gram": "15"
            }
          },
          "analyzer": {
            "synonym_autocomplete": {
              "filter": [
                "standard", "lowercase", "synonym", "autocomplete_filter"],
              "type": "custom",
              "tokenizer": "standard"
            },
            "autocomplete": {
              "filter": ["standard","lowercase","autocomplete_filter"],
              "type": "custom",
              "tokenizer": "standard"
            }
          }
        },
        [...]
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

请查看cross_fields查询文档。您有一个operator参数,如果不存在则设置为OR。这意味着您当前的查询正在搜索字段列表"David Salazar Denver"["first_name","middle_name", "last_name", "city", "county", "street"]的任何字词。这基本上意味着只要在您的任何字段中找到搜索查询中的一个单词,就会从搜索中返回文档。

答案 1 :(得分:0)

Val是正确的,主要问题是cross_fields仅适用于使用相同分析器的字段。

所以我使用下面的代码创建了一个新索引,然后使用reindex API将数据复制到这个新索引

{
  "index": {
    "aliases": {},
    "mappings": {
      "type": {
        "properties": {
          "city": {
            "type": "keyword"
          },
          "county": {
            "type": "text",
            "analyzer": "synonym_autocomplete",
            "search_analyzer": "standard"
          },
          "first_name": {
            "type": "text",
            "analyzer": "synonym_autocomplete",
            "search_analyzer": "standard"
          },
          "last_name": {
            "type": "text",
            "analyzer": "autocomplete",
            "search_analyzer": "standard"
          },
          "middle_name": {
            "type": "text",
            "analyzer": "synonym_autocomplete",
            "search_analyzer": "standard"
          },
          "street": {
            "type": "text",
            "analyzer": "synonym_autocomplete",
            "search_analyzer": "standard"
          },
        }
      }
    },
    "settings": {
      "index": {
        [...]
        "analysis": {
          "filter": {
            "synonym": {
              "type": "synonym",
              "synonyms": [Long list of nicknames]
            },
            "autocomplete_filter": {
              "type": "edge_ngram",
              "min_gram": "2",
              "max_gram": "15"
            }
          },
          "analyzer": {
            "synonym_autocomplete": {
              "filter": [
                "standard", "lowercase", "synonym", "autocomplete_filter"],
              "type": "custom",
              "tokenizer": "standard"
            },
            "autocomplete": {
              "filter": ["standard","lowercase","autocomplete_filter"],
              "type": "custom",
              "tokenizer": "standard"
            }
          }
        },
        [...]
        }
      }
    }
  }
}