问题
Using Hibernate Search 5.9 and elastic server 5.6.10.
I'm trying to persist data from 3 fields into a single field with @Transient annotation. But though the fields shows up in the index structure, the same does not show up when I query the index with curl/chrome. Its not present on the index and the data is lost this way.
Database column: Data
a. address: 123, a street, city, state
Fillr1 : 000
Fillr2: 0
Fillr3: 358
b. address: 234, a street, city, state
Fillr1 : 000
Fillr2: 0
Fillr3: 258
b. address: 567, a street, city, state
Fillr1 : 000
Fillr2: 0
Fillr3: 005
I want to concatenate/or may be not and compare three fields Fillr1 + Fillr2 + Fillr3 to an array of input received from front end application. Lets assume the input array contains data as:
[0000004, 0000005, 0000258, 0000358, 0035648]
I just want to select the row which contains data matching to the input array items.
Till now I've tried filters, must/should queries along with the main select data, below is the latest version of code, I tried. This works for scenario when the input array contains data which is on the elastic server, but If I remove one record which contains data on elastic server, but the same is not present under the input array then also the result set contains data pertaining to the removed record.
agent number e.g. 0000258
BoolQueryBuilder matchQueryBuilder = QueryBuilders.boolQuery();
matchQueryBuilder.must(QueryBuilders.matchQuery("cltseqnum", clt0300to.getCltseqnum()));
matchQueryBuilder.must(QueryBuilders.matchQuery("addrseqnum", clt0300to.getAddrseqnum()));
if(!allAgentsAssignedFlag){
BoolQueryBuilder secondQuery;
for(String agentNum : allUserAgenciesList) {//allUserAgenciesList -> [0000004, 0000005, 0000258, 0000358, 0035648]
secondQuery = QueryBuilders.boolQuery();
secondQuery.must(QueryBuilders.matchQuery("policies.fillr1",agentNum.substring(0, 3)))
.must(QueryBuilders.matchQuery("policies.rpt0agt0nr",agentNum.substring(3, 4)))
.must(QueryBuilders.matchQuery("policies.fillr2",agentNum.substring(4, 7)));
matchQueryBuilder.must(secondQuery);
}
}
Below is the resulted json query:
{
"bool" : {
"must" : [
{
"match" : {
"cltseqnum" : {
"query" : 335,
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"addrseqnum" : {
"query" : 1,
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"should" : [
{
"bool" : {
"must" : [
{
"match" : {
"policies.fillr1" : {
"query" : "000",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.rpt0agt0nr" : {
"query" : "0",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.fillr2" : {
"query" : "004",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
{
"bool" : {
"must" : [
{
"match" : {
"policies.fillr1" : {
"query" : "000",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.rpt0agt0nr" : {
"query" : "0",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.fillr2" : {
"query" : "005",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
{
"bool" : {
"must" : [
{
"match" : {
"policies.fillr1" : {
"query" : "000",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.rpt0agt0nr" : {
"query" : "0",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.fillr2" : {
"query" : "258",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
{
"bool" : {
"must" : [
{
"match" : {
"policies.fillr1" : {
"query" : "000",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.rpt0agt0nr" : {
"query" : "0",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.fillr2" : {
"query" : "358",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
{
"bool" : {
"must" : [
{
"match" : {
"policies.fillr1" : {
"query" : "003",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.rpt0agt0nr" : {
"query" : "5",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match" : {
"policies.fillr2" : {
"query" : "648",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
回答1:
Got a work around for this, used @Transient annotation on getter method of a new custom field(which is not available on the database side) and concatenated my data to it. For more details, please follow below link :
@Transient with @Field annotated field not showing in index on elastic server
来源:https://stackoverflow.com/questions/56545925/select-a-strings-after-concatenating-3-fields-from-index-in-lucenes-boolquerybu