如何解决ElasticSearch - 模糊搜索java api结果不合适?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (1599)

已经在elasticsearch中索引了示例文档,并尝试使用模糊查询进行搜索。但是在使用Java模糊查询api搜索时没有得到任何结果。

请找到我的下面的映射脚本:

PUT productcatalog
 {
    "settings": {
        "analysis": {
            "analyzer": {
                "attr_analyzer": {
                    "type": "custom",
                    "tokenizer": "letter",
                    "char_filter": [
                        "html_strip"
                    ],
                    "filter": ["lowercase", "asciifolding", "stemmer_minimal_english"]
                }
            },
            "filter" : {
                "stemmer_minimal_english" : {
                    "type" : "stemmer",
                    "name" : "minimal_english"
                }
            }
        }
    },
    "mappings": {
        "doc": {
            "properties": {
                "values": {
                    "type": "text",
                    "analyzer": "attr_analyzer"
                },
                "catalog_type": {
                    "type": "text"
                },
                "catalog_id":{
                    "type": "long"
                }
            }
        }
    }
}

查找我的示例数据

PUT productcatalog/doc/1
{
    "catalog_id" : "343",
    "catalog_type" : "series",
    "values" : "Activa Rooftop, valves, VG3000, VG3000FS, butterfly, ball"
}


PUT productcatalog/doc/2
{
    "catalog_id" : "12717",
    "catalog_type" : "product",
    "values" : "Activa Rooftop, valves"
}

找我的搜索脚本:

GET productcatalog/_search
{
    "query": {
        "match" : {
            "values" : {
                "query" : " activa rooftop VG3000",
                "operator" : "and",
                 "boost": 1.0,
                "fuzziness": 2,
                "prefix_length": 0,
                "max_expansions": 100


            }
        }
    }
}

得到以上查询的以下结果:

{
  "took": 239,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.970927,
    "hits": [
      {
        "_index": "productcatalog",
        "_type": "doc",
        "_id": "1",
        "_score": 0.970927,
        "_source": {
          "catalog_id": "343",
          "catalog_type": "series",
          "values": "Activa Rooftop, valves, VG3000, VG3000FS, butterfly, ball"
        }
      }
    ]
  }
}

但是,如果我使用下面的Java API进行相同的模糊搜索,则不会得到任何结果。

请在模糊搜索中找到我的Java API查询:

QueryBuilder qb = QueryBuilders.boolQuery()
               .must(QueryBuilders.fuzzyQuery("values", keyword).boost(1.0f).prefixLength(0).maxExpansions(100));
提问于
用户回答回答于

提到的java查询不是match查询。这是一个must查询,应该用matchQuery而不是boolQuery().must

扫码关注云+社区

领取腾讯云代金券