ElasticSearch未对查询中的单个字符正确排序

emeijp43  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(154)

我无法在ElasticSearch中获得正确的结果。搜索查询将包含以下文本tower A flat no 1103。由于我在搜索查询中指定了tower A,我希望顶部的结果包含tower A,但ElasticSearch的排序是随机的。以下是我的API响应:-

[
    {
        "taskId": "TSK40izdmlm4dmo",
        "taskName": "Granite Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSK40izdmlm4dmo",
                "name": "Granite Work",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Granite Work"
    },
    {
        "taskId": "TSK7nubnw3ytrc9",
        "taskName": "Granite Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSK7nubnw3ytrc9",
                "name": "Granite Work",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Granite Work"
    },
    {
        "taskId": "TSKs3squjd1z447",
        "taskName": "Masonry Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKs3squjd1z447",
                "name": "Masonry Work",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Masonry Work"
    },
    {
        "taskId": "TSK3ps8qgofeius",
        "taskName": "Masonry Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSK3ps8qgofeius",
                "name": "Masonry Work",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Masonry Work"
    },
    {
        "taskId": "TSKgy9przl1vny2",
        "taskName": "Tiling Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKgy9przl1vny2",
                "name": "Tiling Work",
                "highlight": false
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Tiling Work"
    },
    {
        "taskId": "TSKr2q0t09yooav",
        "taskName": "Tiling Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSKr2q0t09yooav",
                "name": "Tiling Work",
                "highlight": false
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Tiling Work"
    },
    {
        "taskId": "TSKj9gr35kqmi3s",
        "taskName": "Water Proffing",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKj9gr35kqmi3s",
                "name": "Water Proffing",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Water Proffing"
    },
    {
        "taskId": "TSKsnc5tydkqoo8",
        "taskName": "Water Proffing",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSKsnc5tydkqoo8",
                "name": "Water Proffing",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Water Proffing"
    },
    {
        "taskId": "TSK3mha5g3qv7a1",
        "taskName": "Inner Plaster",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSK3mha5g3qv7a1",
                "name": "Inner Plaster",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Inner Plaster"
    },
    {
        "taskId": "TSKsq196ob3kstz",
        "taskName": "Untitled subtask",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKsq196ob3kstz",
                "name": "Untitled subtask",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Untitled subtask"
    },
    {
        "taskId": "TSKqu0vs151cmzm",
        "taskName": "Inner Plaster",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSKqu0vs151cmzm",
                "name": "Inner Plaster",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Inner Plaster"
    },
    {
        "taskId": "TSKbsyyti2wga85",
        "taskName": "Carpenter Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKbsyyti2wga85",
                "name": "Carpenter Work",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Carpenter Work"
    },
    {
        "taskId": "TSK0plyn9nbfr26",
        "taskName": "Carpenter Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSK0plyn9nbfr26",
                "name": "Carpenter Work",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Carpenter Work"
    },
    {
        "taskId": "TSKav6rcjbehcpn",
        "taskName": "Putti Second Coat",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKav6rcjbehcpn",
                "name": "Putti Second Coat",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Putti Second Coat"
    },
    {
        "taskId": "TSKmctmqqnogjy5",
        "taskName": "Putti Second Coat",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSKmctmqqnogjy5",
                "name": "Putti Second Coat",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Putti Second Coat"
    },
    {
        "taskId": "TSKvweppvlcqoh6",
        "taskName": "Putti First Coat",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKvweppvlcqoh6",
                "name": "Putti First Coat",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Putti First Coat"
    },
    {
        "taskId": "TSKuav7s4x3j0w8",
        "taskName": "Plumbing Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSKuav7s4x3j0w8",
                "name": "Plumbing Work",
                "highlight": false
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Plumbing Work"
    },
    {
        "taskId": "TSK35iyxzo1n48v",
        "taskName": "Putti First Coat",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSK35iyxzo1n48v",
                "name": "Putti First Coat",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Putti First Coat"
    },
    {
        "taskId": "TSKebpw4u4uj9us",
        "taskName": "Plumbing Work",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower A - 4BHK",
                "_id": "TSKal1o0u24fdwx",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSKcxmewckiw68e",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKy2brl99xvomr",
                "highlight": true
            },
            {
                "_id": "TSKebpw4u4uj9us",
                "name": "Plumbing Work",
                "highlight": false
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Plumbing Work"
    },
    {
        "taskId": "TSK8dkyspduz44n",
        "taskName": "Outer Plaster",
        "workBreakDownStr": [
            {
                "name": "Prabhu Heights",
                "_id": "TSKt5s9u90i5exo",
                "highlight": true
            },
            {
                "name": "Tower B - 3BHK",
                "_id": "TSK3xifvny8eovm",
                "highlight": true
            },
            {
                "name": "11th Floor",
                "_id": "TSK3hr5cdeqs31s",
                "highlight": false
            },
            {
                "name": "Flat no- 1103",
                "_id": "TSKesl2nhhgzgg6",
                "highlight": true
            },
            {
                "_id": "TSK8dkyspduz44n",
                "name": "Outer Plaster",
                "highlight": true
            }
        ],
        "ancestors_combined": "Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Outer Plaster"
    }
]

下面是我使用的查询

const should = [
      {
        match : {
          ancestors_combined : {
            query     : text,
            operator  : 'and',
            fuzziness : 0,
            analyzer  : 'ngram_analyzer',
          },
        },
      },
      {
        match : {
          ancestors_combined : {
            query     : text,
            operator  : 'or',
            fuzziness : 0,
            analyzer  : 'ngram_analyzer',
          },
        },
      },
      {
        match : {
          ancestors_combined : {
            query     : text,
            operator  : 'and',
            fuzziness : 0,
            analyzer  : 'standard',
          },
        },
      },
      {
        match : {
          ancestors_combined : {
            query     : text,
            operator  : 'or',
            fuzziness : 0,
            analyzer  : 'standard',
          },
        },
      },
      {
        match_phrase_prefix : {
          ancestors_combined : {
            query    : text,
            analyzer : 'standard',
          },
        },
      },
    ];

    const query = {
      bool : {
        filter : [
          {
            term : {
              project_id : projectId,
            },
          },
          {
            term : {
              is_active : true,
            },
          },
          {
            term : {
              type : _.taskConstants.TYPE.TASK,
            },
          },
        ],
        should,
      },
    };

这里我使用的是ngram_analyzer和标准分析器。

settings : {
        analysis : {
          analyzer : {
            ngram_analyzer : {
              type      : 'custom',
              tokenizer : 'ngram_tokenizer',
              filter    : ['lowercase', 'ngram_filter'],
            },
          },
          filter : {
            ngram_filter : {
              type     : 'ngram',
              min_gram : 1,
              max_gram : 20,
            },
          },
          tokenizer : {
            ngram_tokenizer : {
              token_chars        : ['letter', 'digit', 'custom'],
              custom_token_chars : '-_.,',
              min_gram           : '1',
              type               : 'edge_ngram',
              max_gram           : '20',
            },
          },
        },
        index : {
          max_ngram_diff : '20',
        },
      },

我正在搜索一个字段ancestors_combined,它由字段ancestors(类型为数组)

ancestors : {
        type       : 'nested',
        properties : {
          _id  : { type: 'keyword' },
          name : {
            type   : 'text',
            fields : {
              keyword : {
                type : 'keyword',
              },
            },
            analyzer : 'ngram_analyzer',
          },
        },
      },
      ancestors_combined : {
        type   : 'text',
        fields : {
          keyword : {
            type         : 'keyword',
            ignore_above : 256,
          },
        },
        analyzer : 'ngram_analyzer',
      },

我尝试提升某些字段,但仍然没有显示在最前面。有人能帮我指出我做错了什么吗?如果需要任何额外的信息,请告诉我。注意:-当我在搜索查询中只键入Tower ATower B时,我会得到正确的结果。同样,workBreakDownStr的响应是字段ancestors

i7uq4tfw

i7uq4tfw1#

Tldr;

我不确定您做错了什么,但是您在should语句中有一个非常复杂的查询。
也许你可以试着简化一下?
我已经为您创建了一个解决方案POC

溶液

设置

PUT /74371488/
{
  "settings": {
    "index.max_ngram_diff": 20,
    "analysis": {
      "analyzer": {
        "ngram_analyzer": {
          "type": "custom",
          "tokenizer": "ngram_tokenizer",
          "filter": [
            "lowercase",
            "ngram_filter"
            ]
        }
      },
      "filter": {
        "ngram_filter": {
          "type": "ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "tokenizer": {
        "ngram_tokenizer": {
          "token_chars": [
            "letter",
            "digit",
            "custom"
            ],
            "custom_token_chars": "-_.,",
            "min_gram": "1",
            "type": "edge_ngram",
            "max_gram": "20"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "ancestors_combined" : {
        "type"   : "text",
        "analyzer" : "ngram_analyzer"
      }
    }
  }
}

POST _bulk
{"index":{"_index":"74371488"}}
{"ancestors_combined":"Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Putti Second Coat"}
{"index":{"_index":"74371488"}}
{"ancestors_combined":"Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Putti First Coat"}
{"index":{"_index":"74371488"}}
{"ancestors_combined":"Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Plumbing Work"}
{"index":{"_index":"74371488"}}
{"ancestors_combined":"Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Putti First Coat"}
{"index":{"_index":"74371488"}}
{"ancestors_combined":"Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Plumbing Work"}
{"index":{"_index":"74371488"}}
{"ancestors_combined":"Prabhu Heights | Tower B - 3BHK | 11th Floor | Flat no- 1103 | Outer Plaster"}

要搜索

GET 74371488/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase_prefix": {
            "ancestors_combined": "tower A"
          }
        }
      ]
    }
  }
}

它只会返回

{
  "took": 481,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 14.130437,
    "hits": [
      {
        "_index": "74371488",
        "_id": "luJrXIQBcn6Q8d0DCdaE",
        "_score": 14.130437,
        "_source": {
          "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Putti Second Coat"
        }
      },
      {
        "_index": "74371488",
        "_id": "meJrXIQBcn6Q8d0DCdaE",
        "_score": 14.130437,
        "_source": {
          "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Putti First Coat"
        }
      },
      {
        "_index": "74371488",
        "_id": "muJrXIQBcn6Q8d0DCdaE",
        "_score": 14.130437,
        "_source": {
          "ancestors_combined": "Prabhu Heights | Tower A - 4BHK | 11th Floor | Flat no- 1103 | Plumbing Work"
        }
      }
    ]
  }
}

相关问题