springElasticSearch查询

chhkpiq4  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(373)

我对这个ElasticSearch和spring boot的世界还很陌生,从过去的2-3天我一直在尝试寻找这个解决方案,遗憾的是我没能找到(可能是因为我对这个还不熟悉)。
我在elastic search中有3列,第一列是id,第二列是name(我正在创建autocomplete api),第三列是数字集numbers。我的用例是,对于第3列中的特定数字,我需要第2列中的自动完成建议。
我用这段代码实现了这一点,它可以工作: repo.findByNumbersAndName(String, String) 但这不允许我设置模糊性、通配符查询和其他搜索参数,所以我尝试使用QueryBuilder,但在构建逻辑方面并不成功(我基本上尝试使用booleanquerybuilder),所以如果有人能在这方面帮助我,那将是非常有帮助的!!
spring boot版本-2.4.2
提前谢谢。
编辑1:-
为了更好地理解我的用例,假设这是我在ElasticSearch中存储的内容

Id | Name   | Numbers |
-----------------------
a1 | ashwin | 1       |
a2 | Ram    | 3       |
a3 | Kumar  | 2       |
a4 | Some   | 2       |
a5 | body   | 1       |
a6 | any    | 3       |
a7 | one    | 4       |
a8 | ashwin | 2       |

现在我应该有控件来指定我的查询,对于这个数字(比如说1),如果输入autocomplete“a”,自动完成的可能性是多少。然后程序应该只搜索数字列中数字为“1”的可能性,因此在这个场景中只有一个输出是“ashwin”。
编辑2:-
我相信我的配置和查询方法和你做的是一样的,我只会粘贴我做的。一个更新是,虽然数据库中的number字段包含整数,但它们存储为string数据类型,这对term query有影响吗?
这是我的elastic-analyzer.json

{

  "analysis": {
    "filter": {
      "autocomplete_filter": {
        "type": "edge_ngram",
        "min_gram": 3,
        "max_gram": 30
      }
    },
    "analyzer": {
      "autocomplete_search": {
        "type": "custom",
        "tokenizer": "standard",
        "filter": [
          "lowercase",
          "stop"

        ]
      },
      "autocomplete_index": {
        "type": "custom",
        "tokenizer": "standard",
        "filter": [
          "lowercase",
          "autocomplete_filter",
          "stop"
        ]
      }
    }
  }
}

我用它来生成一个查询:-

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()

        .must(QueryBuilders.termQuery("Number", number))
        .must(QueryBuilders.matchQuery("Name", search_word));

上面以这种格式生成查询-

{
  "bool" : {
    "must" : [
      {
        "term" : {
          "Number" : {
            "value" : "1",
            "boost" : 1.0
          }
        }
      },
      {
        "match" : {
          "Name" : {
            "query" : "ash",
            "operator" : "OR",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : true,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : true,
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }

这是产生空数组,不明白为什么?我觉得下面的属性相当于您在索引Map中提到的属性

@Id
@Field(type = FieldType.Text)
private String Id;

@Field(type = FieldType.Text, analyzer = "autocomplete_index", searchAnalyzer = "autocomplete_search")
private String Name;

@Field(type = FieldType.Text)
private String Number;

谢谢@escoder
编辑3:-
我也是在@escoder的帮助下偶然发现的,

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()

        .must(QueryBuilders.matchQuery("Number", search_number).boost(1f).operator(Operator.AND))
        .must(QueryBuilders.matchQuery("Name", keyword).boost(0.4f));

这是为elasticsearch生成的查询spring,

{
  "bool" : {
    "must" : [
      {
        "match" : {
          "Number" : {
            "query" : "1",
            "operator" : "AND",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : true,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "match" : {
          "Name" : {
            "query" : "ash",
            "operator" : "OR",
            "fuzziness" : "1",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : true,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : true,
            "boost" : 0.4
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
6fe3ivhb

6fe3ivhb1#

添加索引数据、Map、搜索查询和搜索结果的工作示例
索引Map:

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 4
        }
      },
      "analyzer": {
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "Name": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "standard"
      },
      "Id": {
        "type": "text"
      },
      "Numbers": {
        "type": "integer"
      }
    }
  }
}

索引数据:

{
  "Name": "ashwin",
  "Id": "a1",
  "Numbers": 1
}
{
  "Name": "ashwin",
  "Id": "a1",
  "Numbers": 2
}

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "Name": "a"
          }
        },
        {
          "term": {
            "Numbers": 1
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "66923434",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.2630212,
        "_source": {
          "Name": "ashwin",
          "Id": "a1",
          "Numbers": 1
        }
      }
    ]

更新1:
使用当前的索引Map设置,将为 ashwin ```
GET /_analyze
{
"analyzer" : "autocomplete",
"text" : "ashwin"
}

代币包括:

{
"tokens": [
{
"token": "ash",
"start_offset": 0,
"end_offset": 6,
"type": "",
"position": 0
},
{
"token": "ashw",
"start_offset": 0,
"end_offset": 6,
"type": "",
"position": 0
},
{
"token": "ashwi",
"start_offset": 0,
"end_offset": 6,
"type": "",
"position": 0
},
{
"token": "ashwin",
"start_offset": 0,
"end_offset": 6,
"type": "",
"position": 0
}
]
}

您需要修改索引Map,而不是 `"min_gram": 3` 你需要把它改成 `"min_gram": 1` .
更新2:
你甚至需要改变 `"search_analyzer"` 至 `standard` .

相关问题